scroll ListView To Bottom - Android User Interface

Android examples for User Interface:ListView

Description

scroll ListView To Bottom

Demo Code


import android.annotation.TargetApi;
import android.os.Build;
import android.view.View;
import android.widget.ListView;

public class Main{
    public static void scrollToBottom(ListView listView) {
        scrollToPosition(listView, listView.getAdapter().getCount() - 1, 0);
    }//from  ww w.j  av a  2  s . co  m
    public static void scrollToBottom(ListView listView,
            ScrollToPositionListener listener) {
        scrollToPosition(listView, listView.getAdapter().getCount() - 1, 0,
                listener);
    }
    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public static void scrollToPosition(ListView messageListView,
            int position, int y) {
        scrollToPosition(messageListView, position, y, null);
    }
    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    private static void scrollToPosition(final ListView messageListView,
            final int position, final int y,
            final ScrollToPositionListener listener) {
        messageListView.post(new Runnable() {

            @Override
            public void run() {
                messageListView.setSelectionFromTop(position, y);

                if (listener != null) {
                    listener.onScrollEnd();
                }
            }
        });
    }
}

Related Tutorials