多选回收查看

时间:2018-04-02 11:42:14

标签: android xamarin android-recyclerview multi-select

我想在recyclerview中实现多选。我几乎得到了它,但它有时表现得很奇怪,或者当有很多物品时。

我想有多选功能,在选择中我想要改变项目的背景颜色并恢复其中所有文本视图的文本颜色。

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

以下是代码:

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


}

2 个答案:

答案 0 :(得分:1)

  

您的模型应该

private boolean isSelected = false

    public boolean isSelected() {
        return isSelected;
    }

    public void setSelected(boolean selected) {
        isSelected = selected;
    }
  在onClick事件中

注意:对于最佳练习setTag(position),您的视图位置会被点击并将该标记值用作位置。

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

我认为这可以解决您的问题

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

  

一旦您使用setTag()使用位置操作,将自动选择滚动和位置选择的问题

答案 1 :(得分:0)

在模型类中添加一个额外字段

public boolean isSelected = false;

然后在bindViewHolder之后管理您的检查/取消选中视图。

如果用户点击您的特定视图,您必须像下面的代码一样进行管理。

list.get(position).setSelected(true);
notifyItemChanged(position);

此处,setSelected()是模型类中的方法。