C#Xamarin Edittext单击按钮,然后滚动到ListView项目,How



我列出了一个巨大的listView列表,现在我想让这个用户友好。因此,当我以Edittext键入某些内容时,当我单击按钮时:然后需要以编程方式滚动到ListView项目。我该怎么做?

完成输入后,您需要在列表中搜索该文本,然后将其smotyscroll搜索到该位置。

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);
    };    
}

最新更新