当我在选项卡布局中更改选项卡时,回收站视图项目正在增加

Posted

技术标签:

【中文标题】当我在选项卡布局中更改选项卡时,回收站视图项目正在增加【英文标题】:Recycler View Items are increasing when I change tab in tab layout 【发布时间】:2021-12-04 03:59:05 【问题描述】:

当我从聊天片段更改为通话片段然后回来时,回收站视图的项目增加了一倍有任何解决方案吗? 无论如何请提出建议,因为我无法调试它。当我从聊天选项卡更改为通话选项卡然后再次返回聊天选项卡时,项目正在增加

聊天片段

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()));


ChatListAdapter

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;
//    


【问题讨论】:

【参考方案1】:

可能的情况如下代码 sn-p

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&lt;ChatListModel&gt; list 在类级别声明,当您切换到另一个片段时,onDestroyView 被调用,当您点击返回时,onCreateView 在此处被调用,列表已经保存了以前的数据,因此它正在添加重复数据。

可能的解决方案

仅声明列表变量本地/检查列表是否已有数据,然后不要添加新数据,只需将其设置到适配器即可。

【讨论】:

【参考方案2】:

更新你的 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()));

【讨论】:

以上是关于当我在选项卡布局中更改选项卡时,回收站视图项目正在增加的主要内容,如果未能解决你的问题,请参考以下文章

使用Android ViewPager2在回收器视图中滚动到底部时自动更改选项卡项

如何更改选项卡布局中所选选项卡的形状?

在选项卡布局中使用两窗格片段

使用带有选项卡布局的自定义视图时无法从选项卡中删除填充

更改选项卡时视图更改大小

单击按钮时更改框架布局的内容