我有一个recyclerView
,其中包含已安装应用程序的名称,程序包名称和图标。我已将复选框添加到recyclerView
中,并且由于我在查看一个框并滚动时视图的回收性质,然后框的状态发生了变化。因此,我想知道的是,有什么方法可以保持滚动后的复选框状态吗?我尝试了this,但无法掌握。我没有DataModel
班。
这是我的回收站适配器:
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();
}
}
答案 0 :(得分:0)
不要在OnBindViewHolder中设置Click和getPosition。在ViewHolder中使用它们并获取位置-getAdapterPosition()。祝您好运。
答案 1 :(得分:0)
您可以使用 SparseBooleanArray ,它与地图相似,但它是一个int和boolean键值对,用于存储项目列表中所有项目的状态,然后使用这些键和值在切换选中状态时进行比较。
public static SparseBooleanArray itemCheckBoxState = new SparseBooleanArray();
并在点击处理程序中:
@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);
}
}
和:
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);
}
}