如何在事件点击时仅对gridview中的一行进行着色?

时间:2018-03-10 16:54:34

标签: c# android xamarin xamarin.android

关注代码:

public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    List<string> numbers = new List<string>();

    for (int i = 0; i < 10; i++)
    {
        numbers.Add((i + 1).ToString());
    }

    view = inflater.Inflate(Resource.Layout.fragment_1, container, false);
    gridView = (GridView)view.FindViewById(Resource.Id.grid_1);

    gridView.ChoiceMode = ChoiceMode.Single;

    //Here event click item
    gridView.ItemClick += (sender, e) =>
    { 
        gridView.SetBackgroundColor(Color.ParseColor("#FFC107"));
    };

    ArrayAdapter<string> adapter = new ArrayAdapter<string>(Activity, Resource.Layout.item_1, numbers);

    gridView.Adapter = adapter;

    return view;
}

当我点击1项时,它正在绘制整个gridview。如何仅在点击的位置进行绘制?

关注图片:

enter image description here

当您点击数字5时,会发生这种情况:

enter image description here

我希望你只画5个。任何解决方案?

1 个答案:

答案 0 :(得分:2)

GridView项目点击中,AdapterView.ItemClickEventArgs包含被触摸的View

gridView.ItemClick += (sender, e) =>
{ 
   e.View.SetBackgroundColor(Color.Red);
};