51

I have a ListView with an edit text and a button below it. When I click on a listView item the keyboard appears and push up the edit text and the button. I want the list to scroll to the selected item. Any idea? Thanks

Anita
  • 531
  • 1
  • 4
  • 3
  • 1
    see my answer here, http://stackoverflow.com/a/29345217/1881527 – Melbourne Lopes Apr 01 '15 at 09:52
  • 2
    Possible duplicate of [Programmatically scroll to a specific position in an Android ListView](http://stackoverflow.com/questions/7561353/programmatically-scroll-to-a-specific-position-in-an-android-listview) – Code-Apprentice Aug 18 '16 at 18:46

8 Answers8

75

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

Community
  • 1
  • 1
androidworkz
  • 2,902
  • 1
  • 19
  • 19
  • 17
    This did not work for me. I used smoothScrollToPostion(position) at http://developer.android.com/reference/android/widget/ListView.html#smoothScrollToPosition(int). Important Note: I had to call it after notifyDataSetChanged() was called on the adapter. – SilentNot Oct 02 '13 at 19:41
  • 1
    Note, this one brings you to the position, but as in the other answer, smoothScrollToPosition actually scrolls there. Both work if you want the user to be shown something so it's a matter of how you want it to look. – Richard Fung Apr 28 '14 at 21:53
  • how to make the listview scroll top when only one item exist? – Logan Guo Feb 29 '16 at 08:50
  • @SilentNot This will work, you just have to call after notifyDataSetChanged() and after the changes are done in the listview. Which mean, you will have to call it in a runnable using listView.post(). – Rishabh Sagar May 07 '19 at 10:54
  • 1
    @Rishabh wrong. public int scrollToFile(String fname) { int index = dirContentsAdapter.indexOf(fname); dirContents.smoothScrollToPosition(index); return index; } This works! –  Oct 04 '19 at 02:44
  • But I hate smoothScrollTo... I just need to jump to location. –  Oct 04 '19 at 02:46
47

You could use ListView's smoothScrollToPosition(int position) to scroll to a particular location in the list.

Community
  • 1
  • 1
Rajesh
  • 654
  • 5
  • 8
  • 1
    because of this bug: https://code.google.com/p/android/issues/detail?id=36062, this is actually a better workaround if you want smooth scrolling: http://stackoverflow.com/questions/14479078/smoothscrolltopositionfromtop-is-not-always-working-like-it-should/20997828#20997828 – nommer Aug 04 '14 at 17:18
  • That bug, as described, seems specific to smoothScrollToPositionFromTop. Not sure it affects smoothScrollToPosition... does anyone know? – Sofi Software LLC Feb 06 '15 at 00:30
  • 1
    Don't know, but at time my `smoothScrollToPosition()` does not seem to work, while 'setSelection()' works; though with a jerk. – Darpan Sep 22 '15 at 12:24
28

For a direct scroll:

getListView().setSelection(11);

For a smooth scroll:

getListView().smoothScrollToPosition(11);

To Scroll to top

getListView().setSelectionAfterHeaderView();

Note

try to call it in post because sometime listview is not yet created while you calling it's method

getListView().postDelayed(new Runnable() {          
    @Override
    public void run() {
        lst.setSelection(15);
    }
},100L);
Zar E Ahmer
  • 33,936
  • 20
  • 234
  • 300
6

You should use transcript mode:

getListView().setTranscriptMode(ListView.TRANSCRIPT_MODE_NORMAL);
Jin35
  • 8,602
  • 3
  • 32
  • 52
5

Setup a listener on your the list item being clicked, then use View.getTop() or View.getBottom() when clicked to get it's position within the parent. You can then use ListView.scrollTo(x, y) to scroll to the list item.

Community
  • 1
  • 1
Doge
  • 6,553
  • 5
  • 24
  • 25
  • This is not working for me. If I implement your example the listView is scrolled but the selected item is not centered. – Anita Aug 17 '10 at 15:34
  • 1
    @Anita: You didn't say anything about centering... centering only takes a small modification. Simply add or reduce to the y value in proportion to to screen height. – Doge Aug 19 '10 at 13:11
  • No need to do this tho'. – Martin Marconcini Aug 07 '13 at 23:59
4

Yo can look For

listView.setSelectionFromTop(position, distanceFromHeader);

It will position the Item at position , specified pixels below the top of listview

Pavan Jaju
  • 873
  • 11
  • 19
  • I need to scroll another view simultaneously while scrolling lisview. Using setSelectionFromTop on listview for scrolling blocks the UI thread which makes the scrolling of another view slow. Can you give some suggestion how to do it? – VijayRaj Nov 27 '14 at 09:22
4

You can use

smoothScrollToPosition(position)

Just increase the position of item with 1, and you will get the view of item.

getListView().smoothScrollToPosition(position + 1);
Prashant Kumar Sharma
  • 1,120
  • 1
  • 11
  • 21
2

Using duration gives a better user experience. Use this, with duration added. Will scroll the item in position smoothly to the top of the listview.

int duration = 500;  //miliseconds
int offset = 0;      //fromListTop

listview.smoothScrollToPositionFromTop(position,offset,duration);
  • descrease duration to make scrolling faster
Adam
  • 1,018
  • 1
  • 9
  • 20