Xamarin Android: Select RecycleView row

孤街醉人 提交于 2019-12-11 14:17:39

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!