Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set selected item of ListView without scrolling the view [Android]

Tags:

android

I have a ListView with 10 items in it, when i set the 4th item in the ListView as selected then my ListView gets scrolled and 4th item in the ListView gets placed at the top of the screen.

I don't want to scroll the ListView when i call setSelected() for the list item which is not in the view.

I have also tried to scroll the ListView programmatically using scrollTo(0, 0); but it is not working.

like image 390
User7723337 Avatar asked Mar 26 '12 13:03

User7723337


Video Answer


1 Answers

An old question, but it may be useful to somebody. You can use setSelectionFromTop. It's rather smart, and you can use it for example like that:

getListView().setSelectionFromTop(selection, getListView().getHeight() / 2);

It begins to scroll only after middle of the list:

setSelectionFromTop

like image 156
Kirill Bubochkin Avatar answered Nov 15 '22 03:11

Kirill Bubochkin