【问题标题】: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;
//    }

}

【问题讨论】:

    标签: java android android-fragments android-recyclerview whatsapp


    【解决方案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()));
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-10-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多