recyclerview onscroll的工作原理如何?没有得到预期的结果

时间:2018-01-09 07:52:58

标签: java android android-recyclerview recycler-adapter

我的项目中有一个recyclerview,我已经使用它的适配器在recyclerview中添加了一个联系人列表,在它的viewHolder的onClick中我想将我的联系人列表的每个位置标记为已选中(我在这里使用的是绿色)表示选择),为了做到这一点,我尝试了下面的片段:

@Override
public void onClick(View view) {
    if (selectedItems.get(getAdapterPosition(), false)) {
        selectedItems.delete(getAdapterPosition());
        view.setSelected(false);

        contacts_name.setTextColor(Color.BLACK);
        phone_number.setTextColor(Color.BLACK);
    } else {

        selectedItems.put(getAdapterPosition(), true);
        view.setSelected(true);
        contacts_name.setTextColor(Color.WHITE);
        phone_number.setTextColor(Color.WHITE);

        Log.e("layout ", " Pos " + getLayoutPosition());
        Log.e("Adapter ", " Pos " + getAdapterPosition());
    }
}

所选项目是一个稀疏布尔数组,用于存储联系人每个位置的状态,我在下面使用drawable作为我的布局背景:

<?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@color/selector_green_bg" 
    android:state_pressed="false" android:state_selected="true" />
    <item android:drawable="@color/light_grey_color" 
    android:state_selected="false" />
</selector>

OnBindViewHolder:

@Override
public void onBindViewHolder(ChooseOptionHolder holder, int position) {

    try {
        holder.contacts_name.setText(contact_name_list.get(position));
        holder.phone_number.setText(phone_nums.get(position));
    } catch (Exception e) {
        e.printStackTrace();
    }
}

适配器类

public class ChooseOptionAdapter extends RecyclerView.Adapter<ChooseOptionHolder> {

private ArrayList<String> contact_name_list, phone_nums;
private Context context;
View layoutView;

ChooseOptionAdapter chooseOptionAdapter = this;

public ChooseOptionAdapter(Context context, ArrayList<String> contacts_list, ArrayList<String> phone_num_list) {
    this.contact_name_list = contacts_list;
    this.phone_nums = phone_num_list;
    this.context = context;
}

@Override
public ChooseOptionHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.choose_contact_list, null);
    ChooseOptionHolder rcv = new ChooseOptionHolder(layoutView, chooseOptionAdapter);
    return rcv;
}

@Override
public void onBindViewHolder(ChooseOptionHolder holder, int position) {

    try {
        holder.contacts_name.setText(contact_name_list.get(position));
        holder.phone_number.setText(phone_nums.get(position));
    } catch (Exception e) {
        e.printStackTrace();
    }

    if (holder.selectedItems.get(position)) {
        holder.itemView.setSelected(true);
        holder.contacts_name.setTextColor(Color.WHITE);
        holder.phone_number.setTextColor(Color.WHITE);
    } else {
        holder.itemView.setSelected(false);
        holder.contacts_name.setTextColor(Color.BLACK);
        holder.phone_number.setTextColor(Color.BLACK);
    }
    Log.e("boolean ", "array " + holder.selectedItems);

}

@Override
public int getItemCount() {
    return this.contact_name_list.size();
}}

适配器持有者类

public class ChooseOptionHolder extends RecyclerView.ViewHolder
    implements View.OnClickListener {

public TextView contacts_name;
public TextView phone_number;
int position;

public SparseBooleanArray selectedItems = new SparseBooleanArray();
ArrayList<Boolean> status = new ArrayList<>();
ChooseOptionAdapter chooseOptionAdapter1;

public ChooseOptionHolder(View itemView, ChooseOptionAdapter chooseOptionAdapter) {
    super(itemView);
    itemView.setOnClickListener(this);
    contacts_name = itemView.findViewById(R.id.contacts_name);
    phone_number = itemView.findViewById(R.id.phone_number);

}

@Override
public void onClick(View view) {

    position=getAdapterPosition();

    if (selectedItems.get(position,false)){
        selectedItems.delete(position);
        view.setSelected(false);
        contacts_name.setTextColor(Color.BLACK);
        phone_number.setTextColor(Color.BLACK);
    } else {

        selectedItems.put(position,true);
        view.setSelected(true);
        contacts_name.setTextColor(Color.WHITE);
        phone_number.setTextColor(Color.WHITE);
        Log.e("layout ", " Pos " + position);


    }

}
}

所有这一切都很完美,但问题是当我滚动视图时,视图的其他一些位置已经显示为已选中。我希望有人可以指出我所做的错误。

1 个答案:

答案 0 :(得分:0)

试试这个

@Override
public void onClick(View view) {
    if (selectedItems.get(getAdapterPosition())) {
        selectedItems.delete(getAdapterPosition()); 
    } else {
       selectedItems.put(getAdapterPosition(), true); 
    }

   notifyItemChanged(int position) 
}

@Override
public void onBindViewHolder(ChooseOptionHolder holder, int position) {

    try {
         holder.contacts_name.setText(contact_name_list.get(position));
         holder.phone_number.setText(phone_nums.get(position));

         if(selectedItems.get(position)){
              view.setSelected(true);
              contacts_name.setTextColor(Color.WHITE);
              phone_number.setTextColor(Color.WHITE);
         }else {
             view.setSelected(false);
             contacts_name.setTextColor(Color.BLACK);
             phone_number.setTextColor(Color.BLACK);
         }
   } catch (Exception e) {
    e.printStackTrace();
   }
}