android 將ScrollView滾動到底部


方案1:使用 scrollTo 或 smoothScrollTo 滾動到 scrollview 最后一個節點位置

public static void scrollToBottom(final View scroll, final View inner) {
    Handler handler = new Handler();
    handler.post(new Runnable() {
        public void run() {
            int offset = inner.getMeasuredHeight() - scroll.getHeight();
            if (offset < 0) {
                offset = 0;
            }
            scroll.scrollTo(0, offset);
        }
    });
}

方案2(如scrollview中元素還未加載完全就調用,滾動到底部會失敗。推薦使用方案3):

scrollView.post(new Runnable() {
    public void run() {
        scrollView.fullScroll(View.FOCUS_DOWN);
    }
});

方案3:

scrollView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        scrollView.post(new Runnable() {
            public void run() {
                scrollView.fullScroll(View.FOCUS_DOWN);
            }
        });
    }
});

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM