Recycler View 在执行 multi-Select 时错误地选择了 View Item

Recycler View Wrongly selecting View Item while performing multi-Select

我已经使用 Multi Select 成功地使用 addOnItemTouchListener 实现了一个回收器视图,用于识别和选择使用单和双 click.When 我在回收器视图中有不到 5 个项目,一切正常但是当我有更多数据,确切地说,当回收器视图开始回收视图时,我的选择变得疯狂,它选择了不同的 possition.I 注销以查看点击位置,这些似乎是正确的,但有些地方不对使我的选择 wrong.I 对编程和 android 应用程序 development.Can 很陌生,请大家多多帮助,这是我的代码

我的适配器class

public class ContactAdapter extends RecyclerView.Adapter<ContactAdapter.ContactHolder> implements SectionIndexer, Filterable {

    public List<Contact> contactList;
    List<Contact> filteredUsersList;
    CustomFilter filter;
    Context mContext;
    int itemResource;
    ArrayList<Contact> selected_usersList = new ArrayList<>();
    int pos;

    private ArrayList<Integer> mSectionPositions;

    ContactAdapter(Context mContext, int itemResource, List<Contact> contactList, ArrayList<Contact> selectedList) {
        this.contactList = contactList;
        this.mContext = mContext;
        this.itemResource = itemResource;
        this.selected_usersList = selectedList;
        this.filteredUsersList = contactList;
    }

    @Override
    public ContactHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(itemResource, parent, false);
        return new ContactHolder(v);
    }

    @Override
    public void onBindViewHolder(final ContactHolder holder, int position) {
        pos = position;
        final Contact contact = contactList.get(pos);

        holder.colg.setText(contact.getColg());
        holder.name.setText(contact.getName());
        holder.job.setText(contact.getJob());


        if (contact.getImage() != null)
            holder.img.setImageBitmap(Utility.getPhoto(contact.getImage()));


    }

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


    class ContactHolder extends RecyclerView.ViewHolder {

        private TextView name, colg, job, id, mentee, mentor, participant;
        private ImageView selected;
        // private PorterShapeImageView img;
        private HexagonMaskView img;
        private RelativeLayout rr_layout;
        ItemClickListener itemClickListener;

        ContactHolder(View itemView) {
            super(itemView);
            // Set up the UI widgets of the holder
            // img = (PorterShapeImageView) itemView.findViewById(R.id.contact_image);
            img = (HexagonMaskView) itemView.findViewById(R.id.contact_image);
            name = (TextView) itemView.findViewById(R.id.contact_name);
            colg = (TextView) itemView.findViewById(R.id.contact_colg);
            job = (TextView) itemView.findViewById(R.id.contact_job);
            mentee = (TextView) itemView.findViewById(R.id.mentee);
            mentor = (TextView) itemView.findViewById(R.id.mentor);
            participant = (TextView) itemView.findViewById(R.id.participant);
            rr_layout = (RelativeLayout) itemView.findViewById(R.id.rr_layout);
            selected = (ImageView) itemView.findViewById(R.id.tic_contact_selected);

        }

    }
}

我的 RecyclerItemClickListener Class

public class RecyclerItemClickListener implements RecyclerView.OnItemTouchListener {

    public interface OnItemClickListener {
        void onItemClick(View view, int position);

        void onItemLongClick(View view, int position);
    }

    private OnItemClickListener mListener;
    private GestureDetector mGestureDetector;

    public RecyclerItemClickListener(Context context, final RecyclerView recyclerView, OnItemClickListener listener) {
        mListener = listener;

        mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
            @Override
            public boolean onSingleTapUp(MotionEvent e) {
                return true;
            }

            @Override
            public void onLongPress(MotionEvent e) {
                View childView = recyclerView.findChildViewUnder(e.getX(), e.getY());

                if (childView != null && mListener != null) {
                    mListener.onItemLongClick(childView, recyclerView.getChildAdapterPosition(childView));
                }
            }
        });
    }

    @Override
    public boolean onInterceptTouchEvent(RecyclerView view, MotionEvent e) {
        View childView = view.findChildViewUnder(e.getX(), e.getY());

        if (childView != null && mListener != null && mGestureDetector.onTouchEvent(e)) {
            mListener.onItemClick(childView, view.getChildAdapterPosition(childView));
        }

        return false;
    }

    @Override
    public void onTouchEvent(RecyclerView view, MotionEvent motionEvent) {
    }

    @Override
    public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {

    }
}

我对 addOnItemTouchListener 的实现

    contactsRecyclerViewT.addOnItemTouchListener(new RecyclerItemClickListener(this, contactsRecyclerViewT, new RecyclerItemClickListener.OnItemClickListener() {
        @Override
        public void onItemClick(View view, final int position) {

            Log.e("tag", "" + position);

            for (int i = 0; i < multiselect_list.size(); i++) {
                Log.e("tag", "sss" + multiselect_list.get(i).getName());
            }

            pos = position;

            contact = contactArrayList.get(position);

            menteeTextView = (TextView) view.findViewById(R.id.mentee);
            mentorTextView = (TextView) view.findViewById(R.id.mentor);
            participantTextView = (TextView) view.findViewById(R.id.participant);
            rr_layout = (RelativeLayout) view.findViewById(R.id.rr_layout);
            selected = (ImageView) view.findViewById(R.id.tic_contact_selected);
            img = (HexagonMaskView) view.findViewById(R.id.contact_image);
            name = (TextView) view.findViewById(R.id.contact_name);

            if (isMultiSelect) {

                img.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        multi_select(position);

                        if (multiselect_list.contains(contactArrayList.get(position))) {

                            contact.setStatus("0");
                            Log.e("tag", "setStatus" + contact.getStatus());

                            Log.e("tag", "position " + position);

                            rr_layout.setBackgroundColor(ContextCompat.getColor(ContactsActivity.this, R.color.bg_card_selected));
                            selected.setVisibility(View.VISIBLE);

                            mentorTextView.setBackgroundColor(ContextCompat.getColor(ContactsActivity.this, R.color.colorPrimary));
                            participantTextView.setBackgroundColor(ContextCompat.getColor(ContactsActivity.this, R.color.colorPrimary));
                            menteeTextView.setBackgroundColor(ContextCompat.getColor(ContactsActivity.this, R.color.colorAccent));
                            menteeTextView.setVisibility(View.VISIBLE);
                            mentorTextView.setVisibility(View.VISIBLE);
                            participantTextView.setVisibility(View.VISIBLE);
                            menteeTextView.setClickable(true);
                            mentorTextView.setClickable(true);
                            participantTextView.setClickable(true);

                        } else {
                            rr_layout.setBackgroundColor(ContextCompat.getColor(ContactsActivity.this, R.color.cement));
                            selected.setVisibility(View.GONE);

                            Log.e("tag", "position " + position);

                            menteeTextView.setVisibility(View.GONE);
                            mentorTextView.setVisibility(View.GONE);
                            participantTextView.setVisibility(View.GONE);
                            menteeTextView.setClickable(false);
                            mentorTextView.setClickable(false);
                            participantTextView.setClickable(false);
                        }

                    }
                });

                mentorTextView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        contact.setStatus("1");
                        Log.e("tag", "setStatus" + contact.getStatus());

                        mentorTextView.setBackgroundColor(ContextCompat.getColor(ContactsActivity.this, R.color.colorAccent));
                        menteeTextView.setBackgroundColor(ContextCompat.getColor(ContactsActivity.this, R.color.colorPrimary));
                        participantTextView.setBackgroundColor(ContextCompat.getColor(ContactsActivity.this, R.color.colorPrimary));
                    }
                });


                menteeTextView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        contact.setStatus("0");
                        Log.e("tag", "setStatus" + contact.getStatus());

                        mentorTextView.setBackgroundColor(ContextCompat.getColor(ContactsActivity.this, R.color.colorPrimary));
                        participantTextView.setBackgroundColor(ContextCompat.getColor(ContactsActivity.this, R.color.colorPrimary));
                        menteeTextView.setBackgroundColor(ContextCompat.getColor(ContactsActivity.this, R.color.colorAccent));
                    }
                });


                participantTextView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        contact.setStatus("2");
                        Log.e("tag", "setStatus" + contact.getStatus());

                        mentorTextView.setBackgroundColor(ContextCompat.getColor(ContactsActivity.this, R.color.colorPrimary));
                        menteeTextView.setBackgroundColor(ContextCompat.getColor(ContactsActivity.this, R.color.colorPrimary));
                        participantTextView.setBackgroundColor(ContextCompat.getColor(ContactsActivity.this, R.color.colorAccent));
                    }
                });

                name.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(ContactsActivity.this, OtherProfileActivity.class);
                        intent.putExtra("id", Integer.parseInt(contactArrayList.get(position).getId()));
                        startActivity(intent);
                    }
                });

            } else {
                Intent intent = new Intent(ContactsActivity.this, OtherProfileActivity.class);
                intent.putExtra("id", Integer.parseInt(contactArrayList.get(position).getId()));
                startActivity(intent);
            }
            // contactList.get(position).setStatus("1");
            // contactList.get(position).setStatus("0");

        }

        @Override
        public void onItemLongClick(View view, int position) {

            contact = contactArrayList.get(position);

            Log.e("tag", "" + position);

            for (int i = 0; i < multiselect_list.size(); i++) {
                Log.e("tag", "sss" + multiselect_list.get(i).getName());
            }


            menteeTextView = (TextView) view.findViewById(R.id.mentee);
            mentorTextView = (TextView) view.findViewById(R.id.mentor);
            participantTextView = (TextView) view.findViewById(R.id.participant);
            rr_layout = (RelativeLayout) view.findViewById(R.id.rr_layout);
            selected = (ImageView) view.findViewById(R.id.tic_contact_selected);
            img = (HexagonMaskView) view.findViewById(R.id.contact_image);
            name = (TextView) view.findViewById(R.id.contact_name);

            if (!isMultiSelect) {
                multiselect_list = new ArrayList<>();
                isMultiSelect = true;

                if (mActionMode == null) {
                    mActionMode = startActionMode(mActionModeCallback);
                }
            }

            multi_select(position);

            if (multiselect_list.contains(contactArrayList.get(position))) {

                rr_layout.setBackgroundColor(ContextCompat.getColor(ContactsActivity.this, R.color.bg_card_selected));
                selected.setVisibility(View.VISIBLE);

                contact.setStatus("0");
                Log.e("tag", "setStatus" + contact.getStatus());

                Log.e("tag", "position " + position);

                mentorTextView.setBackgroundColor(ContextCompat.getColor(ContactsActivity.this, R.color.colorPrimary));
                participantTextView.setBackgroundColor(ContextCompat.getColor(ContactsActivity.this, R.color.colorPrimary));
                menteeTextView.setBackgroundColor(ContextCompat.getColor(ContactsActivity.this, R.color.colorAccent));
                menteeTextView.setVisibility(View.VISIBLE);
                mentorTextView.setVisibility(View.VISIBLE);
                participantTextView.setVisibility(View.VISIBLE);
                menteeTextView.setClickable(true);
                mentorTextView.setClickable(true);
                participantTextView.setClickable(true);

            } else {
                rr_layout.setBackgroundColor(ContextCompat.getColor(ContactsActivity.this, R.color.cement));
                selected.setVisibility(View.GONE);

                Log.e("tag", "position " + position);

                menteeTextView.setVisibility(View.GONE);
                mentorTextView.setVisibility(View.GONE);
                participantTextView.setVisibility(View.GONE);
                menteeTextView.setClickable(false);
                mentorTextView.setClickable(false);
                participantTextView.setClickable(false);
            }

        }
    }));

    ArrayList<AlphabetItem> mAlphabetItems = new ArrayList<>();
    List<String> strAlphabets = new ArrayList<>();
    for (int i = 0; i < contactArrayList.size(); i++) {
        Contact contact = contactArrayList.get(i);
        String name = contact.getName();
        if (name == null || name.trim().isEmpty())
            continue;
        String word = name.substring(0, 1);
        if (!strAlphabets.contains(word)) {
            strAlphabets.add(word);
            mAlphabetItems.add(new AlphabetItem(i, word, false));
        }
    }
}

您在 Onclick 侦听器中执行大部分视图操作,您应该将其移至适配器 onBindView,因为即使您在 Onclick 侦听器中设置,它也会在用户滚动时修改。

我没有给出完整的视图操作,我只是给出了我们应该怎么做的提示。

@Override
public void onBindViewHolder(final ContactHolder holder, int position) {
    pos = position;
    final Contact contact = contactList.get(pos);

    holder.colg.setText(contact.getColg());
    holder.name.setText(contact.getName());
    holder.job.setText(contact.getJob());


    if (contact.getImage() != null)
        holder.img.setImageBitmap(Utility.getPhoto(contact.getImage()));


    if (multiselect_list.contains(contactArrayList.get(position))) { // May be you should check form your fragment or actvity using listeners
        holder.mentee.setBackgroundColor(ContextCompat.getColor(ContactsActivity.this, R.color.colorPrimary));
        holder.participant.setBackgroundColor(ContextCompat.getColor(ContactsActivity.this, R.color.colorPrimary));

        holder.mentee.setVisibility(View.VISIBLE);
        holder.participant.setVisibility(View.VISIBLE);
    } else {

        holder.mentee.setVisibility(View.GONE);
        holder.participant.setVisibility(View.GONE);
    }

}

添加布尔数组

boolean [] itemcheck;

使用数组列表在构造函数中初始化。

  itemcheck = new boolean[feedItemList.size()];

像这样签入 bindview holder

 if(itemcheck[position]==true){ 
    holder.row_linearlayout.setBackgroundColor(Color.parseColor("#b7c5ea"));
        }else {
            holder.row_linearlayout.setBackgroundColor(0xFFFFFFFF);
            //holder.row_linearlayout.setBackgroundResource(R.drawable.blurback);
        }

并设置 true 或 false onclick 你的 relativelayout 控件而不是任何其他控件。

 holder.row_linearlayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    LinearLayout Lout = (LinearLayout) v.findViewById(R.id.selectlinear);
                    if(model.isSelected()){
                        Lout.setBackgroundColor(0xFFFFFFFF);
                        itemcheck[position]=false;
                        //get all other controle value here
                    }else{

                        Lout.setBackgroundColor(Color.parseColor("#b7c5ea"));
                        itemcheck[position]=true;     
                      //get all other controle value here                  
                    }
                }
            });