Can We scroll listview on basis of item name on button clicking in android. Like I have list with multiple header and i want to scroll the list to that header on button clicking.
Asked
Active
Viewed 47 times
-1
-
2Possible duplicate of [how to scroll listview to specific position in android programmatically](https://stackoverflow.com/questions/28191413/how-to-scroll-listview-to-specific-position-in-android-programmatically) – Amin Pinjari Oct 04 '19 at 11:46
3 Answers
1
You can get list position by name using below function.
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof YourModel)) return false;
YourModel that = (YourModel) o;
return Objects.equals(itemName, that.itemName);
}
After that you can scroll to particular position using scrollToPosition(position);

Amit Viradiya
- 177
- 9
-
-
is it possible that i can set some value of list's items and scroll the list to that value like i have three button and want to scroll list to specific value set by on each button click – Rahul Korjani Oct 05 '19 at 08:02
-
This is method is for get list position by value. Suppose, you have list with id, name. [ { "id": 1, "name": "abc" }, { "id": 2, "name": "XYZ" } ] Now, you can get "XYZ" position using this function. @Override public boolean equals(Object o) { if (this == o) return true; if (!(o instanceof YourModel)) return false; YourModel that = (YourModel) o; return Objects.equals(name, that.name); } yourList.get("XYZ") => get list position of "XYZ" value. – Amit Viradiya Oct 05 '19 at 10:20
0
For scroll to any postition X:
getListView().setSelection(X);
Smoothly scroll to the specified adapter position.
smoothScrollToPosition(int position)

Chouaib Seghier
- 71
- 3
0
if you are using Recyclerview
then try this
yourRecyclerview.scrollToPosition(position);
And if you are using ListView
then try this
// For smooth scroll:
getListView().smoothScrollToPosition(position);
//For direct scroll
getListView().setSelection(position);

Jakir Hossain
- 3,830
- 1
- 15
- 29