[英]Recycler View Items are increasing when I change tab in tab layout
當我從聊天片段更改為調用片段然后回來時回收器視圖的項目增加了一倍任何解決方案? 請無論如何建議,因為我無法調試它。 當我從聊天選項卡更改為通話選項卡然后再次返回聊天選項卡時,項目會增加
聊天片段
package com.codewithdevesh.cackle.menu;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.codewithdevesh.cackle.PagerAdapter;
import com.codewithdevesh.cackle.R;
import com.codewithdevesh.cackle.adapter.ChatListAdapter;
import com.codewithdevesh.cackle.model.ChatListModel;
import java.util.ArrayList;
import java.util.List;
public class ChatsFragment extends Fragment {
public ChatsFragment() {
}
private List<ChatListModel> list = new ArrayList<>();
private RecyclerView rv;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_chats, container, false);
rv = v.findViewById(R.id.recycler_view);
rv.setLayoutManager(new LinearLayoutManager(getContext()));
getChatList();
return v;
}
private void getChatList() {
list.add(new ChatListModel("11","Devesh","good bye","15/10/2020","https://i.pinimg.com/550x/7d/1a/3f/7d1a3f77eee9f34782c6f88e97a6c888.jpg"));
list.add(new ChatListModel("12","Devesh","good bye","15/10/2020","https://i.pinimg.com/550x/7d/1a/3f/7d1a3f77eee9f34782c6f88e97a6c888.jpg"));
list.add(new ChatListModel("13","Devesh","good bye","15/10/2020","https://i.pinimg.com/550x/7d/1a/3f/7d1a3f77eee9f34782c6f88e97a6c888.jpg"));
list.add(new ChatListModel("14","Devesh","good bye","15/10/2020","https://i.pinimg.com/550x/7d/1a/3f/7d1a3f77eee9f34782c6f88e97a6c888.jpg"));
list.add(new ChatListModel("15","Devesh","good bye","15/10/2020","https://i.pinimg.com/550x/7d/1a/3f/7d1a3f77eee9f34782c6f88e97a6c888.jpg"));
rv.setAdapter(new ChatListAdapter(list,getContext()));
}
}
聊天列表適配器
package com.codewithdevesh.cackle.adapter;
import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import com.codewithdevesh.cackle.R;
import com.codewithdevesh.cackle.model.ChatListModel;
import com.mikhaellopez.circularimageview.CircularImageView;
import java.util.List;
public class ChatListAdapter extends RecyclerView.Adapter<ChatListAdapter.Holder> {
private final List<ChatListModel> list;
private final Context context;
@NonNull
@Override
public ChatListAdapter.Holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(context).inflate(R.layout.chat_list_layout,parent,false);
return new Holder(v);
}
public ChatListAdapter(List<ChatListModel> list, Context context) {
this.list = list;
this.context = context;
}
@Override
public void onBindViewHolder(@NonNull ChatListAdapter.Holder holder, int position) {
ChatListModel chatListModel = list.get(position);
holder.tvName.setText(chatListModel.getUsrName());
holder.tvDesc.setText(chatListModel.getDescription());
holder.tvDate.setText(chatListModel.getDate());
Glide.with(context).load(chatListModel.getUrlProfile()).into(holder.profile);
}
@Override
public int getItemCount() {
return list.size();
}
public static class Holder extends RecyclerView.ViewHolder {
private TextView tvName,tvDesc,tvDate;
private CircularImageView profile;
public Holder(@NonNull View itemView) {
super(itemView);
tvDate = itemView.findViewById(R.id.tv_date);
tvName = itemView.findViewById(R.id.tv_name);
tvDesc = itemView.findViewById(R.id.tv_desc);
profile = itemView.findViewById(R.id.image_profile);
}
}
// public void setReceiverProfileImage(Bitmap bitmap){
// profileImage= bitmap;
// }
}
可能的情況在代碼片段下面
private List<ChatListModel> list = new ArrayList<>();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_chats, container, false);
rv = v.findViewById(R.id.recycler_view);
rv.setLayoutManager(new LinearLayoutManager(getContext()));
getChatList();
return v;
}
在類級別聲明的private List<ChatListModel> list
,當您切換到另一個片段時onDestroyView
被調用,當您單擊返回onCreateView
時,在此處調用列表已經保存了以前的數據,因此它正在添加重復數據。
可能的解決方案
僅在本地聲明列表變量/檢查列表是否已有數據,然后不要添加新數據,只需將其設置到適配器即可。
將您的getChatList()更新為此
private void getChatList() {
list.clear();
list.add(new ChatListModel("11","Devesh","good bye","15/10/2020","https://i.pinimg.com/550x/7d/1a/3f/7d1a3f77eee9f34782c6f88e97a6c888.jpg"));
list.add(new ChatListModel("12","Devesh","good bye","15/10/2020","https://i.pinimg.com/550x/7d/1a/3f/7d1a3f77eee9f34782c6f88e97a6c888.jpg"));
list.add(new ChatListModel("13","Devesh","good bye","15/10/2020","https://i.pinimg.com/550x/7d/1a/3f/7d1a3f77eee9f34782c6f88e97a6c888.jpg"));
list.add(new ChatListModel("14","Devesh","good bye","15/10/2020","https://i.pinimg.com/550x/7d/1a/3f/7d1a3f77eee9f34782c6f88e97a6c888.jpg"));
list.add(new ChatListModel("15","Devesh","good bye","15/10/2020","https://i.pinimg.com/550x/7d/1a/3f/7d1a3f77eee9f34782c6f88e97a6c888.jpg"));
rv.setAdapter(new ChatListAdapter(list,getContext()));
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.