gpt4 book ai didi

android - 回收站 View 中的消息未正确显示

转载 作者:行者123 更新时间:2023-11-29 19:34:41 29 4
gpt4 key购买 nike

我正在编写一个消息传递应用程序,在 RecyclerView 中显示我的消息和其他消息。

白色背景消息是我的消息,彩色消息是其他消息。

当每条消息顺序为“我的消息,其他消息,...”时,回收站 View 显示正确。如下图。 enter image description here

但是当消息序列像“其他消息,其他消息,我的消息”时,回收器 View 将我的消息显示为其他,反之亦然。像下面一样 enter image description here

我将此代码用于 onBindViewHolder

 @Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, final int i) {
View v;
int width = context.getResources().getDisplayMetrics().widthPixels;
LinearLayout.LayoutParams layoutParams;
layoutParams = new LinearLayout.LayoutParams((int) (width * 0.75), ViewGroup.LayoutParams.WRAP_CONTENT);

if(getItemViewType(i) == MINE){
v = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.item_message_view_mine, viewGroup, false);
v.setX((float) (width * 0.23));
}else {
v = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.item_message_view, viewGroup, false);
v.setX(0);
}

layoutParams.setMargins(5, 5, 5, 5);
v.setLayoutParams(layoutParams);

return new ViewHolder(v);
}


@Override
public int getItemViewType(int position) {
Log.e("pos", position + "");
try{
if(items.get(position).isResponse()){
return MINE;
}else {
return OTHERS;
}
}catch (Exception e){
e.printStackTrace();
return OTHERS;
}



}

@Override
public void onBindViewHolder(final ViewHolder viewHolder, int i) {
if(getItemViewType(i) == OTHERS){
if(items.get(i).getResponse() != null){
viewHolder.layout.setBackgroundColors(R.color.green_1, R.color.green_2);
}else {
if(items.get(i).getDate() - System.currentTimeMillis() /1000 > 100){
viewHolder.layout.setBackgroundColors(R.color.red_1, R.color.red_2);
}else {
viewHolder.layout.setBackgroundColors(R.color.yellow_1, R.color.yellow_2);
}
}

}

if(!items.get(i).isResponse()){
if(items.get(i).getResponse() != null){
viewHolder.layout.setBackgroundColors(R.color.green_1, R.color.green_2);
}else {
int type = General.currentUser.getCurrentRole().getType();
int timeOut = 0;
if(type == Role.OPERATOR){
timeOut = General.operatorTimeOut;
}else if(type == Role.EXPERT || type == Role.CO_EXPERT){
timeOut = General.expertTimeOut;
}else if(type == Role.SENDER){
timeOut = General.senderTimeOut;
}else if(type == Role.VERIFIER){
timeOut = General.verifierTimeOut;
}

if((System.currentTimeMillis() / 1000) - items.get(i).getDate() > timeOut){
viewHolder.layout.setBackgroundColors(R.color.red_1, R.color.red_2);
}else {
viewHolder.layout.setBackgroundColors(R.color.yellow_1, R.color.yellow_2);
}
}
}else {
viewHolder.layout.setBackgroundColors(R.color.white, R.color.grey);
}

if(items.get(i).getType() == Message.TYPE_VOICE && items.get(i).getVoice() != null){
viewHolder.layoutVoice.setVisibility(View.VISIBLE);
viewHolder.tvText.setVisibility(View.GONE);
//if(items.get(i).getVoice().get)
File voice = new File(context.getCacheDir().getAbsolutePath() + "/" + items.get(i).getVoice().getId());
if(voice.exists()){
viewHolder.playerView.setVisibility(View.VISIBLE);
viewHolder.ivVoice.setVisibility(View.GONE);
if(playerItem != -1){
if(playerItem == i){
viewHolder.playerView.startAnimation((int) items.get(i).getVoice().getLength(), new PlayerView.PlayerViewListener() {
@Override
public void onPlayerFinished() {

}
});
}else {
viewHolder.playerView.stopAnimation();
}
}
}else {
viewHolder.playerView.setVisibility(View.GONE);
viewHolder.ivVoice.setVisibility(View.VISIBLE);
}

viewHolder.tvVoiceVolume.setText((items.get(i).getVoice().getVolume() / 1024) + " KB");
viewHolder.tvVoiceLength.setText((items.get(i).getVoice().getLength() /60) + ":" + (items.get(i).getVoice().getLength() %60) + " s");


}else {
viewHolder.layoutVoice.setVisibility(View.GONE);
viewHolder.tvText.setVisibility(View.VISIBLE);
viewHolder.tvText.setText(items.get(i).getText());
}

if(items.get(i).isResponse()){
viewHolder.ivSent.setVisibility(View.VISIBLE);
if(items.get(i).getSendStatus() == Message.SENT){
viewHolder.ivSent.setImageResource(R.drawable.ic_check_black_24dp);
viewHolder.ivSent.setColorFilter(ContextCompat.getColor(context, android.R.color.holo_green_dark));
}else if(items.get(i).getSendStatus() == Message.SENDING) {
viewHolder.ivSent.setImageResource(R.drawable.ic_query_builder_black_24dp);
viewHolder.ivSent.setColorFilter(Color.GRAY);
}else if(items.get(i).getSendStatus() == Message.FAILED){
viewHolder.ivSent.setImageResource(R.drawable.ic_error_black_24dp);
viewHolder.ivSent.setColorFilter(Color.RED);
}
}else {
if(items.get(i).getUnder_user_role_process1() > 0){
viewHolder.ivSent.setVisibility(View.VISIBLE);
viewHolder.ivSent.setImageResource(R.drawable.ic_verified_user_black_24dp);
viewHolder.ivSent.setColorFilter(ContextCompat.getColor(context, android.R.color.holo_green_dark));
}else {
viewHolder.ivSent.setVisibility(View.INVISIBLE);
}
}



Date date = new Date(items.get(i).getDate() * 1000L); // *1000 is to convert seconds to milliseconds
SimpleDateFormat sdf = new SimpleDateFormat("MM-dd HH:mm", Locale.ENGLISH); // the format of your date
sdf.setTimeZone(TimeZone.getTimeZone("GMT-4:30")); // give a timezone reference for formatting (see comment at the bottom
String formattedDate = sdf.format(date);

viewHolder.tvDate.setText(formattedDate);

}

有什么问题?

最佳答案

我首先想到的是用户没有类似“UserID”的用户,因此当您在行中键入 2 条消息时,您会遇到一个错误,就像每条消息都在一侧一样。为什么你使用 recyclerView?创建一个类似“ ScrollView ”的布局并为此添加一个 View (消息)不是更好吗?

private long createRandomId() {
char[] chars = "123456789".toCharArray();
StringBuilder sb = new StringBuilder();
Random random = new Random();
for (int i = 0; i < 10; i++) {
char c = chars[random.nextInt(chars.length)];
sb.append(c);
}
return Long.parseLong(sb.toString());
}

关于android - 回收站 View 中的消息未正确显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39364695/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com