这是我的适配器
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++;
}
}
}
}
}
现在在我的活动中初始化适配器
public RecyclerView.Adapter mAdapter;
public RecyclerView mMessagesView;
public List<MessageList> Message_List = new ArrayList<MessageList>();
onCreate 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);
和结果
现在我将清除Message_List
recyclerView也必须为空
清除代码
Message_List.clear();
mAdapter.notifyDataSetChanged();
结果
此处清除后将新项目添加到Messae_List
的问题
立即添加新数据
Message_List.add(some data new); // message i am new data (first)
mAdapter.notifyItemInserted(Message_List.size() - 1);
见结果
它将我的新项目添加到旧的已删除项目中为什么旧项目已经消失了我清除了它回来的列表
让我们尝试添加新的第二条消息
Message_List.add(some data new); // message i am new data (second)
mAdapter.notifyItemInserted(Message_List.size() - 1);
结果
现在添加第三个......数据
结果
答案 0 :(得分:3)
此代码无法解决此问题:
list.remove(position); //remove from list
recycler.removeViewAt(position); //remove view from recyclerview
mAdapter.notifyItemRemoved(position);
但是一旦您在onBindViewHolder
的开始处添加以下行即可完成工作:
holder.setIsRecyclable(false);
答案 1 :(得分:0)
所以不要清除Message_List,只需创建一个新的实例和
NotifyDataSetChanged看到它有bug还是Not ....
或者:
mAdapter.remove(position);
mAdapter.notifyDataSetChanged();
答案 2 :(得分:0)
public void refreshMessages(List<Message_List> messages) {
this.mMessages = messages;
notifyItemRangeChanged(0, mMessages.size());
}
清除所有数据see this
答案 3 :(得分:0)
致电removeAll()
public void removeAll() {
cardItems.clear(); //clear list
notifyDataSetChanged();
}
removeAll()
后,您需要再次初始化recyclerView
。
private void initRecycleView() {
recyclerView.setHasFixedSize(true);
// linear layout manager
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManagerTag);
}
然后使用您想要的数据填充RecyclerView
。
答案 4 :(得分:0)
请参阅此link。
要从recyclerview中正确删除旧项目,请使用以下代码:
list.remove(position); //remove from list
recycler.removeViewAt(position); //remove view from recyclerview
mAdapter.notifyItemRemoved(position);