scroll ScrollView To Bottom - Android User Interface

Android examples for User Interface:ScrollView

Description

scroll ScrollView To Bottom

Demo Code


import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.AbsListView;
import android.widget.ScrollView;

public class Main{
    public static void scrollToBottom(final ScrollView scrollView) {
        if (scrollView != null) {
            scrollView.post(new Runnable() {
                @Override// w w  w.  jav a 2 s .  c  o  m
                public void run() {
                    scrollView.fullScroll(ScrollView.FOCUS_DOWN);
                }
            });
        }
    }
    public static void scrollToBottom(final AbsListView absListView) {
        if (absListView != null) {
            if (absListView.getAdapter() != null
                    && absListView.getAdapter().getCount() > 0) {
                absListView.post(new Runnable() {
                    @Override
                    public void run() {
                        absListView.setSelection(absListView.getAdapter()
                                .getCount() - 1);
                    }
                });
            }
        }
    }
    public static void scrollToBottom(RecyclerView recyclerView) {
        if (recyclerView != null) {
            RecyclerView.LayoutManager layoutManager = recyclerView
                    .getLayoutManager();
            if (recyclerView.getAdapter() != null
                    && recyclerView.getAdapter().getItemCount() > 0) {
                layoutManager.scrollToPosition(recyclerView.getAdapter()
                        .getItemCount() - 1);
            }
        }
    }
}

Related Tutorials