多选回收站视图 [英] Multiselect recyclerview

查看:110
本文介绍了多选回收站视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在recyclerview中实现多选.我几乎了解了它,但有时或当有很多物品时,它的表现却很怪异.

I want to achieve multiselect in recyclerview. I have almost got it but it behaves weirdly at times or when there are many items.

我想拥有多重选择功能,在选择时我想更改项目的背景颜色并还原其中所有文本视图的文本颜色.

I would like to have multiselect function where in on selection I want to change the background color of the item and revert the textcolor of all the text views inside it.

我面临一个奇怪的问题,如果我选择第一个记录并向下滚动,那么即使第8个记录也会被自动选择.如果我选择第二条记录,那么也会选择第9条记录.

I am facing a weird issue, if I select the 1st record and scroll down then even the 8th record gets selected automatically. And if I select 2nd record then 9th one also gets selected.

这是代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Models.ViewModels;
using Android.App;
using Android.Content;
using Android.Graphics;
using Android.OS;
using Android.Runtime;
using Android.Support.V7.Widget;
using Android.Views;
using Android.Widget;

namespace A.Droid.Adapters
{
    public class DeliveryAdapter : RecyclerView.Adapter//, View.IOnClickListener
    {
        List<RequestViewModel> list;
        public Context v;
        public event EventHandler<int> phoneClick;

        List<RequestViewModel> selectedList = new List<RequestViewModel>();

        public DeliveryAdapter(List<RequestViewModel> records, Context v1)
        {
            list = records;
            v = v1;
        }
        public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
        {
            // Inflate the CardView for the photo:
            View itemView = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.activity_cardview_slinkyRecordList, parent, false);

            DeliveryListViewHolder vh = new DeliveryListViewHolder(itemView, OnPhoneClick);
            return vh;
        }

        // Fill in the contents of the photo card (invoked by the layout manager):
        public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
        {
            DeliveryListViewHolder viewHolder = holder as DeliveryListViewHolder;
            viewHolder.PICNumber.Text = string.IsNullOrEmpty(list[position].PICNumber) ? "-" : list[position].PICNumber;
            viewHolder.PropertyName.Text = string.IsNullOrEmpty(list[position].PropertyName) ? "-" : list[position].PropertyName;
            viewHolder.ContactAddress.Text = string.IsNullOrEmpty(list[position].ContactAddress) ? "-" : list[position].ContactAddress;
            viewHolder.ContactPerson.Text = string.IsNullOrEmpty(list[position].ContactPerson) ? "-" : list[position].ContactPerson;
            viewHolder.ContactNumber.Text = string.IsNullOrEmpty(list[position].ContactNumber) ? "-" : list[position].ContactNumber;

            viewHolder.NumberOfAliveSpecies.Text = list[position].NumberOfAliveStock + " Alive " + list[position].NameOfSpecies;
            viewHolder.NumberOfDeadSpecies.Text = list[position].NumberOfDeadStock + " Dead " + list[position].NameOfSpecies;

            viewHolder.DistanceOfTransporterToPIC.Text = list[position].DistanceOfTransporterFromPIC.ToString() + " KM"; //DeliveryList[position].DistanceOfTransporterFromPIC.ToString();
                                                                                                                         //viewHolder.MainLinearLayout.SetOnClickListener((new OnClickListener(viewHolder.MainLinearLayout,position));                                                                         // cardViewList.Add(viewHolder.cardView); //add all the cards to this list
                                                                                                                         //viewHolder.MainLinearLayout.SetOnClickListener(this);                                                                        // cardViewList.Add(viewHolder.cardView); //add all the cards to this list

            viewHolder.cardView.Click += delegate (object sender, EventArgs e)
            {
                if (selectedList.All(i => i.slinkyStockRequestId != list[position].slinkyStockRequestId))
                {
                    selectedList.Add(list[position]);

                    viewHolder.MainLinearLayout.SetBackgroundColor(v.Resources.GetColor(Resource.Color.white));

                    viewHolder.PICNumber.SetTextColor(v.Resources.GetColor(Resource.Color.dark_blue));
                    viewHolder.PropertyName.SetTextColor(v.Resources.GetColor(Resource.Color.dark_blue));
                    viewHolder.ContactAddress.SetTextColor(v.Resources.GetColor(Resource.Color.dark_blue));
                    viewHolder.ContactPerson.SetTextColor(v.Resources.GetColor(Resource.Color.dark_blue));
                    viewHolder.ContactNumber.SetTextColor(v.Resources.GetColor(Resource.Color.dark_blue));

                    viewHolder.NumberOfAliveSpecies.SetTextColor(v.Resources.GetColor(Resource.Color.dark_blue));
                    viewHolder.NumberOfDeadSpecies.SetTextColor(v.Resources.GetColor(Resource.Color.dark_blue));

                    viewHolder.DistanceOfTransporterToPIC.SetTextColor(v.Resources.GetColor(Resource.Color.dark_blue));
                    viewHolder.mapIcon.SetColorFilter(v.Resources.GetColor(Resource.Color.dark_blue), PorterDuff.Mode.SrcAtop);
                    viewHolder.contactIcon.SetColorFilter(v.Resources.GetColor(Resource.Color.dark_blue), PorterDuff.Mode.SrcAtop);
                    viewHolder.phoneIcon.SetColorFilter(v.Resources.GetColor(Resource.Color.dark_blue), PorterDuff.Mode.SrcAtop);

                }
                else
                {
                    selectedList.Remove(list[position]);

                    viewHolder.MainLinearLayout.SetBackgroundColor(v.Resources.GetColor(Resource.Color.dark_blue));

                    viewHolder.PICNumber.SetTextColor(v.Resources.GetColor(Resource.Color.white));
                    viewHolder.PropertyName.SetTextColor(v.Resources.GetColor(Resource.Color.white));
                    viewHolder.ContactAddress.SetTextColor(v.Resources.GetColor(Resource.Color.white));
                    viewHolder.ContactPerson.SetTextColor(v.Resources.GetColor(Resource.Color.white));
                    viewHolder.ContactNumber.SetTextColor(v.Resources.GetColor(Resource.Color.white));

                    viewHolder.NumberOfAliveSpecies.SetTextColor(v.Resources.GetColor(Resource.Color.white));
                    viewHolder.NumberOfDeadSpecies.SetTextColor(v.Resources.GetColor(Resource.Color.white));

                    viewHolder.DistanceOfTransporterToPIC.SetTextColor(v.Resources.GetColor(Resource.Color.white));
                    viewHolder.mapIcon.SetColorFilter(v.Resources.GetColor(Resource.Color.white), PorterDuff.Mode.SrcAtop);
                    viewHolder.contactIcon.SetColorFilter(v.Resources.GetColor(Resource.Color.white), PorterDuff.Mode.SrcAtop);
                    viewHolder.phoneIcon.SetColorFilter(v.Resources.GetColor(Resource.Color.white), PorterDuff.Mode.SrcAtop);

                }
            };



            animate(holder);
        }


        public List<RequestViewModel> GetSelectedItems()
        {
            return selectedList;
        }


        // Return the number of photos available in the photo album:
        public override int ItemCount
        {
            get { return list.Count; }
        }


        // Raise an event when the phone-click takes place:
        void OnPhoneClick(int position)
        {
            if (phoneClick != null)
            {
                phoneClick(this, position);
            }
        }


        public class DeliveryListViewHolder : RecyclerView.ViewHolder
        {
            public ImageView MapTag { get; private set; }
            public TextView PICNumber { get; private set; }
            public TextView PropertyName { get; private set; }
            public TextView ContactAddress { get; private set; }
            public TextView ContactPerson { get; private set; }
            public TextView ContactNumber { get; private set; }
            public TextView NameOfSpecies { get; private set; }
            public TextView NumberOfAliveSpecies { get; private set; }
            public TextView NumberOfDeadSpecies { get; private set; }
            public TextView DistanceOfTransporterToPIC { get; private set; }
            public CardView cardView { get; private set; }
            public LinearLayout MainLinearLayout { get; private set; }
            public ImageView mapIcon { get; private set; }
            public ImageView contactIcon { get; private set; }
            public ImageView phoneIcon { get; private set; }
            // Get references to the views defined in the CardView layout.
            public DeliveryListViewHolder(View itemView, Action<int> phoneClickListener) : base(itemView)
            {
                MapTag = itemView.FindViewById<ImageView>(Resource.Id.mapIcon);
                PICNumber = itemView.FindViewById<TextView>(Resource.Id.PICNumber);
                PropertyName = itemView.FindViewById<TextView>(Resource.Id.nameOfProperty);
                ContactPerson = itemView.FindViewById<TextView>(Resource.Id.contactPerson);
                NumberOfAliveSpecies = itemView.FindViewById<TextView>(Resource.Id.noOfAliveSpecies);
                NumberOfDeadSpecies = itemView.FindViewById<TextView>(Resource.Id.noOfDeadSpecies);
                DistanceOfTransporterToPIC = itemView.FindViewById<TextView>(Resource.Id.areaInKM);
                ContactAddress = itemView.FindViewById<TextView>(Resource.Id.address);
                ContactNumber = itemView.FindViewById<TextView>(Resource.Id.mobileNo);
                mapIcon = itemView.FindViewById<ImageView>(Resource.Id.mapIcon);
                contactIcon = itemView.FindViewById<ImageView>(Resource.Id.contactIcon);
                phoneIcon = itemView.FindViewById<ImageView>(Resource.Id.phoneIcon);

                cardView = itemView.FindViewById<CardView>(Resource.Id.mainCardviewLayout);
                MainLinearLayout = itemView.FindViewById<LinearLayout>(Resource.Id.MainLinearLayout);
                ContactNumber.Click += (sender, e) => phoneClickListener(base.Position);
            }
        }
    }


}

推荐答案

您的模型应该具有

private boolean isSelected = false

    public boolean isSelected() {
        return isSelected;
    }

    public void setSelected(boolean selected) {
        isSelected = selected;
    }

在您的onClick事件中,

注意:最佳做法是将setTag(position)与您要点击的视图的位置一起使用,并将该标记值用作位置.

Note: For Best practice setTag(position) with your position to your view that is getting clicked and use that tag value as a position.

int pos = (int) view.getTag(); 
list[pos].setSelected(!list[pos].isSelected());
                        notifyItemChanged(position);

我认为这将解决您有关的问题

我选择第一个记录并向下滚动,然后自动选择第8个记录.如果我选择第二条记录,那么也会选择第9条记录.

一旦您将通过setTag()使用和使用position进行操作,就消除了滚动和位置自动选择的问题

Once you will use and operate using position with setTag() this issue of scrolling and position getting selected automatically will be eliminated

这篇关于多选回收站视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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