Go to list item in AlertDialog without selection
I have a huge list of items that I present to an AlertDialog. I would like to present the user with a list scrolling down to the most likely area from which they will select one item. I use
AlertDialog.Builder.setSingleChoiceItems(myAdapter, ...).
ArrayAdapter<MyType> myAdapter;
The problem I am having a hard time dealing with is how to scroll the element if it is not logically correct to represent the selected element.
I tried to get ListView
from the resultant AlertDialog
. But it is empty (even after Builder
creating and showing it).
I tried to get populated to ListView
inflate the plane ListView
in res / layout. listView.scrollTo(x, y)
doesn't seem to have an effect.
I tried to install OnShowListener
for AlertDialog. onShow
() is never called.
Does anyone know about work?
source to share
You can use the functions that come with the ListView class:
smoothScrollByOffset(int offset);
or
smoothScrollToPosition(int position);
Or
If you want to scroll one by one, you can use functions like:
private void scrollToNext() {
int currentPosition = getListView().getFirstVisiblePosition();
if (currentPosition == getListView().getCount() - 1)
return;
getListView().setSelection(currentPosition + 1);
getListView().clearFocus();
}
private void scrollToPrevious() {
int currentPosition = getListView().getFirstVisiblePosition();
if (currentPosition == 0)
return;
getListView().setSelection(currentPosition - 1);
getListView().clearFocus();
}
source to share