ScrollView实现滚动效果

一、实现滚动效果

ScrollView的种类:
HorizontalScrollView:水平滚动视图
ScrollView:垂直滚动视图
隐藏ScrollView
1.标签属性:android:scrollbars = "none"
2.代码设置:
setHorizontalScrollBarEnabled(false);隐藏横向ScrollView
setVerticalScrollBarEnabled(false);隐藏纵向ScrollView

二、监听ScrollView何时滑动到底部

监听ScrollView何时滑到底部setOnTouchListener()
scrollView.getChildAt(0).getMeasuredHeight():内容总高度
scrollView.getHeight():一屏幕的高度
scrollView.getScrollY():滚动条滑动的高度
scrollView = (ScrollView) findViewById(R.id.scroll);
scrollView.setOnTouchListener(new OnTouchListener() {

@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_MOVE:
if (scrollView.getChildAt(0).getMeasuredHeight()
<= scrollView.getHeight()+scrollView.getScrollY()) {
Log.i("tag", "滑动到底部");
Log.i("scrollView.getChildAt(0).getMeasuredHeight()", ""+scrollView.getChildAt(0).getMeasuredHeight());
Log.i("scrollView.getHeight()", ""+scrollView.getHeight());
Log.i("scrollView.getScrollY()", ""+scrollView.getScrollY());
text.append(getResources().getString(R.string.scorll_content));
}
break;

default:
break;
}
return false;
}
});

三、控制ScrollView视图位置

scrollView的两种滚动方法:
scrollTo(x,y):以视图其实位置开始计算。(常用于记录书签)
scrollBy(x,y):相对第一次的位置,去滚动对应的距离

你可能感兴趣的:(Android初学)