滚动时重用视图时,在 RecyclerView 中保存所选项目的状态
Save state of the item selected in RecyclerView when view is reused while scrolling
我已经为 recyclerView 创建了 Adapter 和 ViewHolder。我将 itemView 的 imageButton 绑定到 Viewholder 中。并在 onBindViewHolder 中设置了一个 onClickListener。
一切都按预期工作,但现在的问题是,当我向下滚动列表时,所选项目的 imageButton 的选定状态发生变化,列表底部的一些项目已经出现
选择。
这是一些代码
public class ListViewHolder extends RecyclerView.ViewHolder {
TextView textViewUserName;
ImageButton imageButtonCheckMark;
public ListViewHolder(View itemView) {
super(itemView);
textViewUserName = (TextView) itemView.findViewById(R.id.textView_user_name);
imageButtonCheckMark = (ImageButton) itemView.findViewById(R.id.imageButton_add_user);
}
}
适配器class
public class ListAdapter extends RecyclerView.Adapter<ListAdapter.ListViewHolder> {
private LayoutInflater inflater;
private Context context;
private List<Metadata> list;
public static boolean isUserSelected = false;
ListInterface listener;
public ListAdapter(Context context, List<Metadata> data, ListInterface listener) {
Log.d(TAG, "Passed list to adapter : " + data.size());
inflater = LayoutInflater.from(context);
this.context = context;
this.list = data;
this.listener = listener;
}
@Override
public ListViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.item_following_people, parent, false);
return new ListViewHolder(view);
}
@Override
public void onBindViewHolder(ListViewHolder holder, int position) {
holder.textViewUserName.setText(list.get(position).name);
holder.imageButtonCheckMark.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!isUserSelected) {
holder.imageButtonCheckMark.setImageResource(R.drawable.checkmark_green);
isUserSelected = true;
} else {
holder.imageButtonCheckMark.setImageResource(R.drawable.checkmark_grey);
isUserSelected = false;
}
listener.onUserSelected(isUserSelected, holder.getLayoutPosition());
}
});
}
@Override
public int getItemCount() {
return list.size();
}
}
界面class
public interface ListInterface {
public void onUserSelected(boolean userStatus, int listPosition);
}
内部接口回调监听activity
// Interface callback listener implemented in the activity class
private ArrayList<Metadata> metadataList = new ArrayList<>();
ArrayList<String> selectedUserIdList = new ArrayList<>();
@Override
public void onUserSelected(boolean isUserSelected, int pos) {
if (isUserSelected) {
Log.d(TAG, "Selected user Id: " + metadataList.get(pos).userID);
followersSelectedIdList.add(metadataList.get(pos).userID);
} else {
Log.d(TAG, "Removed user Id: " + metadataList.get(pos).userID);
followersSelectedIdList.remove(metadataList.get(pos).userID);
}
}
您看到此行为是因为视图在您滚动时被重复使用(回收)。您需要保存列表中所有按钮的状态,并在 onBindViewHolder() 中设置每个按钮的状态,因为它变得可见。
在您的模型中添加一个属性 class 将在您的按钮是否被选中时保存状态,在 onBindViewHolder() 中编写代码来管理这样的按钮视图,
if(yourModel.getIsSelected()){
// positive view
} else {
//negative view
}
我已经为 recyclerView 创建了 Adapter 和 ViewHolder。我将 itemView 的 imageButton 绑定到 Viewholder 中。并在 onBindViewHolder 中设置了一个 onClickListener。
一切都按预期工作,但现在的问题是,当我向下滚动列表时,所选项目的 imageButton 的选定状态发生变化,列表底部的一些项目已经出现 选择。
这是一些代码
public class ListViewHolder extends RecyclerView.ViewHolder {
TextView textViewUserName;
ImageButton imageButtonCheckMark;
public ListViewHolder(View itemView) {
super(itemView);
textViewUserName = (TextView) itemView.findViewById(R.id.textView_user_name);
imageButtonCheckMark = (ImageButton) itemView.findViewById(R.id.imageButton_add_user);
}
}
适配器class
public class ListAdapter extends RecyclerView.Adapter<ListAdapter.ListViewHolder> {
private LayoutInflater inflater;
private Context context;
private List<Metadata> list;
public static boolean isUserSelected = false;
ListInterface listener;
public ListAdapter(Context context, List<Metadata> data, ListInterface listener) {
Log.d(TAG, "Passed list to adapter : " + data.size());
inflater = LayoutInflater.from(context);
this.context = context;
this.list = data;
this.listener = listener;
}
@Override
public ListViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.item_following_people, parent, false);
return new ListViewHolder(view);
}
@Override
public void onBindViewHolder(ListViewHolder holder, int position) {
holder.textViewUserName.setText(list.get(position).name);
holder.imageButtonCheckMark.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!isUserSelected) {
holder.imageButtonCheckMark.setImageResource(R.drawable.checkmark_green);
isUserSelected = true;
} else {
holder.imageButtonCheckMark.setImageResource(R.drawable.checkmark_grey);
isUserSelected = false;
}
listener.onUserSelected(isUserSelected, holder.getLayoutPosition());
}
});
}
@Override
public int getItemCount() {
return list.size();
}
}
界面class
public interface ListInterface {
public void onUserSelected(boolean userStatus, int listPosition);
}
内部接口回调监听activity
// Interface callback listener implemented in the activity class
private ArrayList<Metadata> metadataList = new ArrayList<>();
ArrayList<String> selectedUserIdList = new ArrayList<>();
@Override
public void onUserSelected(boolean isUserSelected, int pos) {
if (isUserSelected) {
Log.d(TAG, "Selected user Id: " + metadataList.get(pos).userID);
followersSelectedIdList.add(metadataList.get(pos).userID);
} else {
Log.d(TAG, "Removed user Id: " + metadataList.get(pos).userID);
followersSelectedIdList.remove(metadataList.get(pos).userID);
}
}
您看到此行为是因为视图在您滚动时被重复使用(回收)。您需要保存列表中所有按钮的状态,并在 onBindViewHolder() 中设置每个按钮的状态,因为它变得可见。
在您的模型中添加一个属性 class 将在您的按钮是否被选中时保存状态,在 onBindViewHolder() 中编写代码来管理这样的按钮视图,
if(yourModel.getIsSelected()){
// positive view
} else {
//negative view
}