美文网首页Android升云的Android记事本
Android-使用ScrollView实现布局自动滚动

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

作者: 升云手札 | 来源:发表于2017-09-11 19:49 被阅读0次

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 = new Handler();
3、实现一个Runnable

    /**
     * 滚屏的线程
     */
    private Runnable ScrollRunnable = new Runnable() {

        @SuppressLint("NewApi")
        @Override
        public void run() {
            // TODO Auto-generated method stub
            int off = mLayout.getMeasuredHeight() - mScrollView.getHeight();// 判断高度
            if (off > 0) {
                mScrollView.scrollBy(0, 50);
                if (mScrollView.getScaleY() == off) {
                    Thread.currentThread().interrupt();
                } else {
                        mHandler.postDelayed(this, 1000);
                }
            }
        }
    };

4、 开始滚动
mHandler.post(ScrollRunnable);
5、 暂停滚动
mHandler.removeCallbacks(ScrollRunnable);

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

相关文章

网友评论

    本文标题:Android-使用ScrollView实现布局自动滚动

    本文链接:https://www.haomeiwen.com/subject/ttnfsxtx.html