0

I have a recyclerView that contains the name, package name and icon of installed apps. I have added checkbox to the recyclerView and because of recycling nature of the view when I check a box and scroll then then the state of box changes. So, what I wanted to know is that is there any way to keep the checkbox state as it is after scrolling? I tried this but not able to grasp. I do not have DataModel class. Here is my recycler Adapter :

public class AppsAdapter extends RecyclerView.Adapter<AppsAdapter.ViewHolder>{

    Context context1;
    List<String> stringList;

    public AppsAdapter(Context context, List<String> list){

        context1 = context;

        stringList = list;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder{

        public CardView cardView;
        public ImageView imageView;
        public TextView textView_App_Name;
        public TextView textView_App_Package_Name;
        public CheckBox checkBox;


        public ViewHolder (View view){

            super(view);

            this.setIsRecyclable(false);

            cardView = (CardView) view.findViewById(R.id.card_view);
            imageView = (ImageView) view.findViewById(R.id.imageview);
            textView_App_Name = (TextView) view.findViewById(R.id.Apk_Name);
            textView_App_Package_Name = (TextView) view.findViewById(R.id.Apk_Package_Name);
            checkBox = (CheckBox)view.findViewById(R.id.cb);

        }
    }

    @Override
    public AppsAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType){

        View view2 = LayoutInflater.from(context1).inflate(R.layout.cardview_layout,parent,false);

        ViewHolder viewHolder = new ViewHolder(view2);

        return viewHolder;
    }

    @Override
    public void onBindViewHolder(final ViewHolder viewHolder, final int position){

        ApkInfoExtractor apkInfoExtractor = new ApkInfoExtractor(context1);

        final String ApplicationPackageName = (String) stringList.get(position);
        String ApplicationLabelName = apkInfoExtractor.GetAppName(ApplicationPackageName);
        Drawable drawable = apkInfoExtractor.getAppIconByPackageName(ApplicationPackageName);

        viewHolder.textView_App_Name.setText(ApplicationLabelName);

        viewHolder.textView_App_Package_Name.setText(ApplicationPackageName);

        viewHolder.imageView.setImageDrawable(drawable);

        viewHolder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                //
            }
        });

    }



    @Override
    public int getItemCount(){

        return stringList.size();
    }


}
plase
  • 361
  • 1
  • 3
  • 9
  • create a array of boolen which store the checkbox state and apply it in onBindViewHolder function. set listener setOnCheckedChangeListener in view holder class and use getAdapterPosition to know position. – Jaydeep Devda Jul 07 '18 at 13:29

2 Answers2

1

You can use SparseBooleanArray which is similar to a map but is a key value pair of int and boolean to store the state of all the items in your list of items and then use the keys and values to compare when toggling the checked state.

    public static SparseBooleanArray itemCheckBoxState = new SparseBooleanArray();

and in the click handler:

@Override
        public void onClick(View v) {
            int adapterPosition = getAdapterPosition();
            if (!itemCheckBoxState .get(adapterPosition, false)) {
                mCheckedTextView.setChecked(true);
                itemCheckBoxState .put(adapterPosition, true);
            }
            else  {
                mCheckedTextView.setChecked(false);
                itemCheckBoxState .put(adapterPosition, false);
            }
}

and:

    ViewHolder(View itemView) {
        super(itemView);
        mCheckedTextView = (CheckedTextView) itemView.findViewById(R.id.checked_text_view);
        itemView.setOnClickListener(this);
    }

    void bind(int position) {
        if (!itemCheckBoxState .get(position, false)) {
            mCheckedTextView.setChecked(false);}
        else {
            mCheckedTextView.setChecked(true);
        }

}

Amirhosein
  • 4,266
  • 4
  • 22
  • 35
0

don't setClick and getPosition in OnBindViewHolder.use them in ViewHolder and get position - getAdapterPosition().good luck for you.

TieuNhi
  • 29
  • 4