C#Xamarin editText点击按钮,然后滚动到listview项目,怎么样?

时间:2016-06-20 18:02:15

标签: c# android listview scroll xamarin

我制作了一个巨大的列表视图列表,现在我想让这个用户友好。因此,当我在edittext中输入内容并单击按钮时:然后它需要以编程方式滚动到listview项目。我怎么能这样做?

1 个答案:

答案 0 :(得分:0)

完成输入后,您需要在列表中搜索该文本并平滑滚动到该位置。

private ListView _listView;
private ArrayAdapter<string> _adapter;
private EditText _inputSearch;
private Button _buttonSearch;

protected override void OnCreate(Bundle bundle)
{
    base.OnCreate(bundle);    
    SetContentView(Resource.Layout.Main);

    string[] products = {"Winter Is Coming", "The Kingsroad", "Lord Snow", "Cripples, Bastards, and Broken Things", "The Wolf and the Lion", "A Golden Crown", "You Win or You Die", "The Pointy End", "Baelor", "Fire and Blood"};

    _listView = FindViewById<ListView>(Resource.Id.list_view);
    _inputSearch = FindViewById<EditText>(Resource.Id.inputSearch);
    _buttonSearch = FindViewById< EditText > (Resource.Id.btnSearch);
    _adapter = new ArrayAdapter<string>(this, Resource.Layout.list_item, Resource.Id.product_name, products);
    _listView.Adapter = _adapter;

    _buttonSearch.Click += (sender, e) => 
    {             
        var index = Array.FindIndex(products, i => i.Equals(_inputSearch.Text));
        _listView.SmoothScrollToPosition(index);
    };    
}