回收者视图执行多重选择时错误地选择了“查看项目" [英] Recycler View Wrongly selecting View Item while performing multi-Select

查看:143
本文介绍了回收者视图执行多重选择时错误地选择了“查看项目"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用addOnItemTouchListener成功实现了Multi Select的回收器视图,可通过单击和双击进行识别和选择.当我在回收器视图中的项目少于5个时,一切正常,但是当我有更多数据时,确切地说当回收者视图开始回收视图时,我的选择变得疯狂,并且选择了不同的位置.我注销以查看单击位置,这些位置似乎正确,但某些地方不正确,这使我的选择出错了.我是新手编程和Android应用程序开发.任何人都可以在这方面帮到我很多,这是我的代码

I have implemented an recycler view with Multi Select successfully using addOnItemTouchListener for identifying and selecting using single and double click.When i have Less than 5 items int the recycler view,Everything works fine but when i have more data,to be exact when the recycler view starts recycling the view,my selection is going crazy and it selects differnt possition.I logged out to see the clicking position,those seems to be right but something is not right which makes my selection wrong.I'm new to programming and to android app development.Can anyone take alot at this and help me out please,Here is my code

我的适配器类

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类

My 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的实现

My Implementation of 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侦听器中进行了设置,当用户滚动时.

You are doing most of the view manipulation in Onclick listener, you should move that to you adapter onBindView, because even if you set in Onclick listener, it will modify when user scroll.

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

I am not giving complete view manipulation, I am just giving the hint how we should do.

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

}

这篇关于回收者视图执行多重选择时错误地选择了“查看项目"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆