我在cardview中有一个带有linearlayout和textviews的回收站视图。我想为我的项目实现onclick方法,我想在其中更改线性布局的背景颜色并反转textviews的textcolor。我有它的工作,但我面临的主要问题是,如果我选择第一项,那么第一项和第七项的颜色都会改变。
试着让这个工作从两天开始。任何帮助将不胜感激。
这是我的代码:
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);
}
}
}
}
修改
从Deep Patel
输入,这是我的代码:
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;
}
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)
{
setSelectedPosition(position);
};
viewHolder.MainLinearLayout.SetBackgroundColor(list[position].isSelected() ? v.Resources.GetColor(Resource.Color.white) : v.Resources.GetColor(Resource.Color.dark_blue));
animate(holder);
}
private void setSelectedPosition(int position)
{
for (int i = 0; i < list.Count(); i++)
{
list[position].setSelected(i == position);
}
NotifyDataSetChanged();
}
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);
}
}
}
}
答案 0 :(得分:1)
在您的模型中, 获取布尔变量,并创建getter setter方法
private boolean isSelected = false
public boolean isSelected() {
return isSelected;
}
public void setSelected(boolean selected) {
isSelected = selected;
}
您OnBindViewHolder
中的
viewHolder.MainLinearLayout.SetBackgroundColor(list[position].isSelected()?v.Resources.GetColor(Resource.Color.white):v.Resources.GetColor(Resource.Color.black));
在onClick事件中, //编辑以下代码
注意:对于最佳做法setTag(position)
,您的视图位置会被点击并将该标记值用作位置。
int pos = (int) view.getTag();
list[pos].setSelected(!list[pos].isSelected());
notifyItemChanged(position);
答案 1 :(得分:0)
您需要在OnBindViewHolder()
重置视图的背景颜色,因为这里会重复使用视图
在OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
if (selectedList.All(i => i.slinkyStockRequestId != list[position].slinkyStockRequestId))
viewHolder.MainLinearLayout.SetBackgroundColor(v.Resources.GetColor(Resource.C olor.white));
else
viewHolder.MainLinearLayout.SetBackgroundColor(v.Resources.GetColor(Resource.Color.dark_blue));