gpt4 book ai didi

android - RecyclerView 清除列表并添加新项目也显示旧项目

转载 作者:行者123 更新时间:2023-12-05 00:17:21 24 4
gpt4 key购买 nike

这是我的适配器

 public class MessageAdapter1 extends RecyclerView.Adapter<MessageAdapter1.ViewHolder> {

private List<MessageList> mMessages;
private int[] mUsernameColors;
private Context context;

public MessageAdapter1(Context context, List<MessageList> messages) {
mMessages = messages;
this.context = context;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
int layout = -1;

int type = -1;
if(mMessages.get(viewType).getDir().equals("left")) type = 1;
else if(mMessages.get(viewType).getDir().equals("right")) type = 0;
else if(mMessages.get(viewType).getDir().equals("typing")) type = 2;


switch (type) {
case MessageList.TYPE_MESSAGE_RIGHT:
layout = R.layout.right_message;
break;
case MessageList.TYPE_MESSAGE_LEFT:
layout = R.layout.left_message;
break;
case MessageList.TYPE_ACTION:
layout = R.layout.message_left;
break;
}

View v = LayoutInflater.from(parent.getContext()).inflate(layout, parent, false);

return new ViewHolder(v);
}

@Override
public void onBindViewHolder(ViewHolder viewHolder, int position) {
MessageList message = mMessages.get(position);


viewHolder.setGroupMessage(message);
}

@Override
public int getItemCount() {
return mMessages.size();
}



@Override
public int getItemViewType(int position) {

return position;
}




public class ViewHolder extends RecyclerView.ViewHolder {

private LinearLayout groupMessage;

public ViewHolder(View itemView) {
super(itemView);

groupMessage = (LinearLayout)itemView.findViewById(R.id.messages);

}

public void setGroupMessage(MessageList m) {


if (null == groupMessage) return;

int i = 0;

if(m.getMessageStatus() == false){
m.setMessageStatus(true);
for (String message : m.getMessageList()) {

//TextView text = new TextView(activity);
TextView text = new MyTextView(context);
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
p.setMargins(0, 0, 0, 2);

if (m.getDir().equals("left")) {

text.setTextColor(Color.BLACK);
p.gravity = Gravity.LEFT;
if(m.getMessageList().size() == 1){
text.setBackgroundResource(R.drawable.message_left_default);
}
else if (i == 0) {
text.setBackgroundResource(R.drawable.message_left_first);
} else if (i + 1 == m.getMessageList().size()) {
text.setBackgroundResource(R.drawable.message_left_last);
} else {
text.setBackgroundResource(R.drawable.message_left);
}


} else{

p.gravity = Gravity.RIGHT;
text.setTextColor(Color.WHITE);

if(m.getMessageList().size() == 1){
text.setBackgroundResource(R.drawable.message_right_default);
}
else if (i == 0) {
text.setBackgroundResource(R.drawable.message_right_first);
} else if (i + 1 == m.getMessageList().size()) {
text.setBackgroundResource(R.drawable.message_right_last);
} else {
text.setBackgroundResource(R.drawable.message_right);
}

}


text.setLayoutParams(p);
text.setText(message);
text.setPadding(8, 8, 8, 8);

text.setTextSize(18f);



//text.setTextAppearance(context, android.R.style.TextAppearance_Large);
groupMessage.addView(text);
i++;
}

}
}
}
}

现在在我的 Activity 中初始化适配器

public  RecyclerView.Adapter mAdapter;
public RecyclerView mMessagesView;
public List<MessageList> Message_List = new ArrayList<MessageList>();

创建 Activity

    mAdapter = new MessageAdapter1(getApplicationContext(), Message_List);
mMessagesView = (RecyclerView) findViewById(R.id.messages);
mMessagesView.setLayoutManager(new LinearLayoutManager(this));
mMessagesView.setAdapter(mAdapter);

向 Message_List 添加一些数据

Message_List.add(data);
mAdapter.notifyItemInserted(Message_List.size() - 1);

和结果

enter image description here

现在我将清除Message_List recyclerView也必须为空

清晰的代码

 Message_List.clear();
mAdapter.notifyDataSetChanged();

结果

enter image description here

这里是清除Messae_List后添加新项目时出现的问题

立即添加新数据

 Message_List.add(some data new); // message i am new data (first)
mAdapter.notifyItemInserted(Message_List.size() - 1);

查看结果

enter image description here

它将我的新项目附加到旧的已删除项目中,为什么旧​​项目消失了,我从它回来的地方清除了列表

让我们尝试添加新的第二条消息

 Message_List.add(some data new); // message i am new data (second)
mAdapter.notifyItemInserted(Message_List.size() - 1);

结果

enter image description here

现在添加第三个和第四个......数据

结果

enter image description here

最佳答案

这段代码没有解决这个问题:

list.remove(position); //remove from list
recycler.removeViewAt(position); //remove view from recyclerview
mAdapter.notifyItemRemoved(position);

但是,一旦您在 onBindViewHolder 的开头添加以下行,就完成了工作:

holder.setIsRecyclable(false);

关于android - RecyclerView 清除列表并添加新项目也显示旧项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38682740/

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