Programmatically Scroll to a Specific Position in an Android Listview

how to scroll listview to specific position in android programmatically

Try this:

listview_nearby_locations.setSelection(9);

Programmatically set position in ListView without scrolling

There is a method in AbsListView, called smoothScrollToPositionFromTop() and it takes duration parameter. So if you set it to 0, you may do it without scrolling, animation.

smoothScrollToPositionFromTop.

How to scroll to an item at specific position (that is not in view), in recycler view in android?

Use below code:

yourRecyclerViewObject.getLayoutManager().scrollToPosition(itemPosition);

How to scroll to a specific list position inside AlertDialog

You can use AlertDialog to get the list and after show you can scroll to a certain position.

Try this way it should work .

fun createAlert(activity: Activity, mandatory: Boolean, provinceSelected: (Int) -> Unit) {
val provinceDialog: AlertDialog.Builder = AlertDialog.Builder(activity)
provinceDialog.setTitle(activity.getString(R.string.select_province_from_list))
provinceDialog.setCancelable(!mandatory)
val arrayAdapter = ArrayAdapter<String>(activity, R.layout.province_list_row_item, SubAdministrativeAreas.provincesList)
provinceDialog.setAdapter(arrayAdapter
) { _, positionSelected ->
provinceSelected(positionSelected)
}
val dialog = provinceDialog.create()
dialog.setOnShowListener {
dialog.listView.smoothScrollToPosition(11)
}
dialog.show()
}

How can I set the initial scroll position of my list?

calling

listview.post(new Runnable() {

@Override
public void run() {
listview.setSelection(mSelectedPosition);
}
});

seems to have helped others.



Related Topics



Leave a reply



Submit