Home > Software design >  Recycler View Items are increasing when I change tab in tab layout
Recycler View Items are increasing when I change tab in tab layout

Time:10-16

when I change from chat fragment to call fragment and then come back the items of recycler view got increased by double any solution? please suggest anyway as I am not able to debug it. The items are increasing when I change from chat tab to call tab and then again come back to chat tab

Chat Fragment

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

}

CodePudding user response:

Possible case is below code snippet

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 declared at class level and when you switching to another fragment onDestroyView getting called and when you click back onCreateView get called here list already holding previous data so it is getting added duplicate data.

Possible solution

Declare list variable local only/ check if list has already data then do not add new data just set it to the adapter.

CodePudding user response:

Update your getChatList() to this

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()));
}
  • Related