How to scroll ListView to the bottom?
Asked Answered
K

10

14

I though this would be a simple task, but apparently there is no way to scroll listview to the bottom. All solutions that I found are using variations of setSelection(lastItem) method which only sets selection to last item, but does not scrolls to the bottom of it.

In my case I have a empty listview (with a long empty view header set) and I want to scroll to bottom of it.

So, is there a way to do it?

Edit:

So for those who are interested the working solution is:

getListView().setSelectionFromTop(0, -mHeader.getHeight());

and

getListView().scrollTo(mOffset)

This also works, with right offset (calculated based on current scroll position), but might give you some unexpected results.

Klinges answered 25/9, 2013 at 5:34 Comment(2)
possible duplicate of Android scroll to the bottom of a listviewJester
refer this best solution if you are developing an chatting app #3607030Orthography
C
-2

Try using list.scrollTo(0, list.getHeight());

Casualty answered 25/9, 2013 at 5:37 Comment(2)
This method causes overscroll - listview is extended (with lot's of empty space at bottom) and the moment you try to scroll it it shrinks back.Klinges
Ok, the direction was right you just need to scroll with a right offset.Klinges
S
38

If you want the list view to be scrolled always at the bottom even when you are updating the list view dynamically then you can add these attributes in list view itself.

android:stackFromBottom="true"
android:transcriptMode="alwaysScroll"
Spigot answered 3/5, 2014 at 7:40 Comment(0)
T
19

use the following

    lv.setSelection(adapter.getCount() - 1);
Trevor answered 25/9, 2013 at 5:45 Comment(3)
Please read my question again, there is only one child in the listivew and I want to scroll to the bottom of it. There is no adapter, only empty view (view set through lv.setEmptyView(view)).Klinges
Sorry, not empty view, but header.Klinges
sorry mate no idea for headerTrevor
B
11

If you would like to have a scroll animation programmatically you could easily scroll to the bottom of the list using:

listView.smoothScrollToPosition(adapter.getCount());

scrolling to the top most of the list can be achieved using:

listView.smoothScrollToPosition(0);
Briareus answered 11/2, 2014 at 0:25 Comment(0)
Z
3

Try this one.. it will solve your problem, i tried it and it works great.

   listView.post(new Runnable(){
             public void run() {
             listView.setSelection(listView.getCount() - 1);
    }});
Zouave answered 14/8, 2014 at 20:46 Comment(0)
C
3
private void scrollMyListViewToBottom() {
    myListView.post(new Runnable() {
        @Override
        public void run() {
            // Select the last row so it will scroll into view...
            myListView.setSelection(myListAdapter.getCount() - 1);
        }
    });
}

Its working for me

Copley answered 25/11, 2016 at 7:22 Comment(0)
O
1

Use following and try.

listview.setSelection(adapter.getCount()-1);

Obscuration answered 25/9, 2013 at 5:47 Comment(1)
Please read my question again, there is only one child in the listivew and I want to scroll to the bottom of it. There is no adapter, only empty view (view set through lv.setEmptyView(view)).Klinges
S
1

You might want to try myListView.smoothScrollToPosition(myListView.getCount() - 1). This way, you're not forced into selecting the last row. Plus, you get some smooth, beautiful scrolling! (:

Schoolbook answered 14/6, 2015 at 23:12 Comment(0)
J
1

I Had the same problem, This works best for me

listView.setSelection(listView.getAdapter().getCount()-1);
Jealous answered 22/12, 2015 at 8:0 Comment(0)
F
0

if there is no adapter set to your ListView then it has no children at all, empty view is not a child of your ListView

Flak answered 25/9, 2013 at 6:37 Comment(1)
Yes, sorry, I got it a little bit wrong here, there is situation where there are only empty view (no problems here), and when there are only header, which counts as children (need to scroll to it's bottom).Klinges
C
-2

Try using list.scrollTo(0, list.getHeight());

Casualty answered 25/9, 2013 at 5:37 Comment(2)
This method causes overscroll - listview is extended (with lot's of empty space at bottom) and the moment you try to scroll it it shrinks back.Klinges
Ok, the direction was right you just need to scroll with a right offset.Klinges

© 2022 - 2024 — McMap. All rights reserved.