Checkbox Changing State When Scrolling In Recyclerview
I made a small app that shows me a list of all the apps installed to my phone I want to add a check box to every cardview so that I can mark my favorite apps and other classificati
Solution 1:
That is because in the recycler view, item views are re used to show new data. You have to add a variable in your data item that holds the state of the checkbox at a particular position. Then in the onBindViewHolder
you can check the value of the tracking variable and set the state of the checkbox like yourCheckbox.setchecked(item.getSelected())
set the tracking variable value in the onCheckChanged
method
Solution 2:
set view onclicklistener
in BindViewholder
method so you can use the current position of item as in
privateSparseBooleanArraysba=newSparseBooleanArray();
@OverridepublicvoidonBindViewHolder(ViewHolder viewHolder,finalint position){
viewHolder.setIsRecyclable(false);
ApkInfoExtractorapkInfoExtractor=newApkInfoExtractor(context1);
finalStringApplicationPackageName= (String) stringList.get(position);
//calling apps name and iconStringApplicationLabelName= apkInfoExtractor.GetAppName(ApplicationPackageName);
Drawabledrawable= apkInfoExtractor.getAppIconByPackageName(ApplicationPackageName);
//setting app name and icon for every card
viewHolder.textView_App_Name.setText(ApplicationLabelName);
viewHolder.imageView.setImageDrawable(drawable);
viewHolder.itemView.setOnClickListener(newView.OnClickListener() {
@OverridepublicvoidonClick(View v) {
sba.put(position,!sba.get(position));
notifyDataSetChanged();
}
});
viewHolder.checkBox.setChecked(sba.get(position));
}
Solution 3:
Set checkbox inside checkBox onclickListener
holder.mCheckBox.setOnClickListener(newView.OnClickListener() {
@OverridepublicvoidonClick(View view) {
CheckBoxcheckBox= (CheckBox) view;
mDataModel = (DataModel) checkBox.getTag();
mDataModel.setChecked(checkBox.isChecked());
arrayList.get(position).setChecked(checkBox.isChecked());
}
});
Post a Comment for "Checkbox Changing State When Scrolling In Recyclerview"