ListView scrolls the selected item.

I have a ListView with edit text and a button below it. When I click on the listView element, the keyboard appears and clicks the edit text and button. I want the list to move to the selected item. Any ideas? Thanks

+44
android
Aug 17 '10 at 13:50
source share
8 answers

You can use the ListView setSelection(int position) method to scroll to a row.

+69
Aug 17 '10 at 18:51
source share

You can use ListView smoothScrollToPosition(int position) to jump to a specific location in the list.

+42
Feb 27 '13 at 7:35
source share

For direct scrolling:

 getListView().setSelection(11); 

For smooth scrolling:

 getListView().smoothScrollToPosition(11); 

Scroll up

 getListView().setSelectionAfterHeaderView(); 

Note

try to call it in the message, because once the listview has not yet been created, when you call it with the method

 getListView().postDelayed(new Runnable() { @Override public void run() { lst.setSelection(15); } },100L); 
+21
May 13 '15 at 7:12
source share

Set up a listener on the selected list item, then use View.getTop() or View.getBottom() when clicked to get its position in the parent element. Then you can use ListView.scrollTo(x, y) to jump to the list item.

+4
Aug 17 '10 at 14:40
source share

You should use transcript mode :

 getListView().setTranscriptMode(ListView.TRANSCRIPT_MODE_NORMAL); 
+4
Jun 25 '12 at 17:57
source share

You may be looking for

listView.setSelectionFromTop (position, distanceFromHeader);

It positions the item at the position indicated by the pixels below the top of the list.

+3
Mar 21 '13 at 13:39
source share

Using duration gives a better user interface. Use this, with added duration. Scroll the item at the position smoothly at the top of the list.

 int duration = 500; //miliseconds int offset = 0; //fromListTop listview.smoothScrollToPositionFromTop(position,offset,duration); 
  • decrease scroll time faster
+1
Jul 03 '15 at 1:53 on
source share

you can use

 smoothScrollToPosition(position) 

Just increase the position of the element from 1, and you get a view of the element.

 getListView().smoothScrollToPosition(position + 1); 
0
Sep 05 '17 at 5:47 on
source share



All Articles