1. 程式人生 > >利用ScrollView實現佈局自動滾動

利用ScrollView實現佈局自動滾動

首先1,獲得ScrollView sc = (ScrollView) findViewById(R.id.scroll);//scroll物件
     LinearLayout mlayout = (LinearLayout) findViewById(R.id.mlayout);//scrollView中包含的佈局物件
2,定義一個Handler
private final Handler mHandler = new Handler();
3,實現一個執行緒

        private Runnable ScrollRunnable= new Runnable() {
                @Override
                public void run() {
                         int off = mlayout.getMeasuredHeight() - sc.getHeight();//判斷高度      
 if (off > 0) {
                                sc.scrollBy(0, 30);
                                if (sc.getScrollY() == off) {
                                        Thread.currentThread().interrupt();
                                } else {
                                        mHandler.postDelayed(this, 1000);
                                }
                        }
                }
        };

4,開始滾動
mHandler.post(ScrollRunnable);

5,暫停滾動
mHandler.removeCallbacks(ScrollRunnable);

ScrollView強制滑到底部
scroll.fullScroll(View.FOCUS_DOWN)