Android-使用ScrollView实现布局自动滚动
2017-09-11 本文已影响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)