Recycler View执行多选时错误选择查看项目

时间:2017-07-27 05:36:14

标签: android android-recyclerview ontouchlistener recycler-adapter gesturedetector

我已经使用addOnItemTouchListener成功实现了Multi Select的回收视图,用于识别和选择使用单击和双击。当我在回收者视图中有少于5个项目时,一切正常但是当我有更多数据时,确切地说当回收者视图开始回收视图时,我的选择变得疯狂,它选择了不同的位置。我退出以查看点击位置,这些似乎是正确的但是有些东西不对,这使得我的选择错误。我' m新的编程和Android应用程序开发。任何人都可以在这里采取很多帮助我,这是我的代码

我的适配器类

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类

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

2 个答案:

答案 0 :(得分:2)

添加boolena数组

boolean [] itemcheck;

在构造函数中用你的arraylist初始化。

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

像这样检查bindview持有者

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

并设置你的相对布局控制的ture或false onclick而不是任何其他的控制。

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

答案 1 :(得分:1)

你正在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);
    }

}