Sver
Sver

Reputation: 3409

How to scroll ListView to the bottom?

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.

Upvotes: 14

Views: 36296

Answers (10)

Pradip
Pradip

Reputation: 316

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

Upvotes: 3

Usman Shaikh
Usman Shaikh

Reputation: 31

I Had the same problem, This works best for me

listView.setSelection(listView.getAdapter().getCount()-1);

Upvotes: 1

Hunter S
Hunter S

Reputation: 1311

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! (:

Upvotes: 1

HTU
HTU

Reputation: 1034

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);
    }});

Upvotes: 3

Rana Ranvijay Singh
Rana Ranvijay Singh

Reputation: 6155

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"

Upvotes: 38

Aryo
Aryo

Reputation: 4508

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);

Upvotes: 11

pskink
pskink

Reputation: 24720

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

Upvotes: 0

Pratik Dasa
Pratik Dasa

Reputation: 7439

Use following and try.

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

Upvotes: 1

WISHY
WISHY

Reputation: 11999

use the following

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

Upvotes: 19

Maxim Efimov
Maxim Efimov

Reputation: 2737

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

Upvotes: -2

Related Questions