在滚动时重复使用视图时,保存在RecyclerView中选择的项目的状态

时间:2016-10-19 22:00:02

标签: android android-recyclerview recycler-adapter android-viewholder onitemclick

我为recyclerView创建了一个Adapter和ViewHolder。我将viewView的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);
    }
}

适配器类

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

Inteface类

public interface ListInterface {
    public void onUserSelected(boolean userStatus, int listPosition);
}

活动内部的接口回调侦听器

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

2 个答案:

答案 0 :(得分:1)

您会看到此行为,因为在滚动时会重复使用(回收)视图。您需要保存列表中所有按钮的状态,并在onBindViewHolder()中的每个按钮上设置状态,因为它变得可见。

答案 1 :(得分:1)

在模型类中添加一个属性,在选择或不选择按钮时保存状态,在onBindViewHolder()中编写代码来管理按钮视图,

if(yourModel.getIsSelected()){
   // positive view
} else { 
   //negative view
}