Android-使用ScrollView实现布局自动滚动

1、  获得ScrollView对象和ScrollView中包含的布局对象

mScrollView = (ScrollView) findViewById(R.id.l_rp_ques_main_scrollview);

mLayout = (LinearLayout) findViewById(R.id.l_rp_ques_main_scrolllayout);

2、定义一个Handler

private final mHandler = newHandler();

 

3、  实现一个线程

 1     /**

 2      * 滚屏的线程

 3      */

 4     private Runnable ScrollRunnable = new Runnable() {

 5 

 6         @SuppressLint("NewApi")

 7         @Override

 8         public void run() {

 9             // TODO Auto-generated method stub

10             int off = mLayout.getMeasuredHeight() - mScrollView.getHeight();// 判断高度

11             if (off > 0) {

12                 mScrollView.scrollBy(0, 50);

13                 if (mScrollView.getScaleY() == off) {

14                     Thread.currentThread().interrupt();

15                 } else {

16                         mHandler.postDelayed(this, 1000);

17                 }

18             }

19         }

20     };

4、  开始滚动

mHandler.post(ScrollRunnable);

5、  暂停滚动

mHandler.removeCallbacks(ScrollRunnable);

 

6、ScrollView强制滑到底部 
mScrollView.fullScroll(View.FOCUS_DOWN)

你可能感兴趣的:(scrollview)