简体   繁体   中英

C# Xamarin editText click on button and then scroll to listview item, how?

I made a huge listview list, now I want to make this user friendly. So when I type something in a edittext and when I click on the button: then It needs to scroll programmatically to the listview item. How can I do this?

When you finish typing you need to search that text in your list and smoothscroll to that position.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM