问题
I want to select highlighting with a color a RecycleView row. I tried most of the online example for Xamarin Android but without success.
How can I do it?
回答1:
I want to select high light with a color a RecycleView row.
RecyclerView does not handle item selection so you need do it yourself, add the following code in your RecyclerView.Adapter :
int selectedPosition = -1;
public override void OnBindViewHolder(RecyclerView.ViewHolder viewHolder, int position)
{
(viewHolder as ViewHolder).TextView.SetText(dataSet [position],TextView.BufferType.Normal);
if (selectedPosition == position)
viewHolder.ItemView.SetBackgroundColor(Color.Pink);
else
viewHolder.ItemView.SetBackgroundColor(Color.Transparent);
viewHolder.ItemView.Click += (sender, e) =>
{
selectedPosition = position;
NotifyDataSetChanged();
};
}
Effect.
来源:https://stackoverflow.com/questions/47607726/xamarin-android-select-recycleview-row