public RecyclerView.Adapter mAdapter;
public RecyclerView mMessagesView;
this is my list
public List<MessageList> Message_List = new ArrayList<MessageList>();
and this is my adapter
mAdapter = new MessageAdapter1(getApplicationContext(), Message_List);
mMessagesView = (RecyclerView) findViewById(R.id.messages);
mMessagesView.setLayoutManager(new LinearLayoutManager(this));
mMessagesView.setAdapter(mAdapter);
now i add some data on it
Message_List.add(messageList);
mAdapter.notifyItemInserted(Message_List.size() - 1);
its work without problems
now i want to change all Message_List
data and show in in recyvlerView
i do this
public void swap(List<MessageList> datas){
Message_List.clear();
Message_List.addAll(datas);
mAdapter.notifyDataSetChanged();
}
here i replace all data in Message_List
and notifyDataSetChanged
now its shown new data of Message_List
empty
Message_List
show blank
but when i try add new item to list and notify adapter its shown to me some of old item i removed it from Message_List
and in LOG
i watch it no old data there
now this is image before update Message_List
data
now change Message_List
data and notify
like we see blank because there are no data on Message_List
now try add new item to Message_List
and notify adapter
Message_List.add(balabala data);
mAdapter.notifyItemInserted(Message_List.size() - 1);
and result must be without old data
for more information this is my MessageList
class
public class MessageList {
public static final int TYPE_MESSAGE_RIGHT = 0;
public static final int TYPE_MESSAGE_LEFT = 1;
public static final int TYPE_ACTION = 2;
public static final String TYPING = "typing";
private String message, thumbnailUrl;
private String date;
private int user_id;
private boolean status = false;
private String fname;
private String direction;
private ArrayList<String> messageList;
public MessageList() {
setMessageStatus(status);
}
public MessageList(String name, String thumbnailUrl, String date, String direction,
ArrayList<String> messageList, String fname, int user_id) {
this.message = name;
this.thumbnailUrl = thumbnailUrl;
this.date = date;
this.direction = direction;
this.messageList = messageList;
this.fname = fname;
this.user_id = user_id;
}
public String getMessage() {
return message;
}
public String getFname() {
return fname;
}
public void setMessage(String message) {
this.message = message;
}
public String getThumbnailUrl() {
return thumbnailUrl;
}
public void setThumbnailUrl(String thumbnailUrl) {
this.thumbnailUrl = thumbnailUrl;
}
public String getDate() {
return date;
}
public boolean getMessageStatus() {
return status;
}
public void setMessageStatus(boolean status) {
this.status = status;
}
public int getUser_id() {
return user_id;
}
public void setUser_id(int user_id) {
this.user_id = user_id;
}
public void setDate(String date) {
this.date = date;
}
public void setFname(String fname) {
this.fname = fname;
}
public String getDir() {
return direction;
}
public void setDir(String direction) {
this.direction = direction;
}
public ArrayList<String> getMessageList() {
return messageList;
}
public void setMessageList(ArrayList<String> messageList) {
this.messageList = messageList;
}
}
and this is my adapter
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;
//private ImageView typing;
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++;
}
}
}
}
}