ScrollView滚动到底部和顶部的检测
2013-08-08 14:57
141 查看
本文链接 http://write.blog.csdn.net/postedit/9831793
1. 源起
很多分页加载的组件都是继承自ScrollView,分页要检查是否滚动到底部,下拉刷新需要检测是否滚动到顶部。
2. 代码
3. 说明
检测滚动的时机
上面是在ScrollView的onTouchListener里面检测,如果是继承ScrollView,也可以在onTouchEvent或者onInterceptTouchEvent中判断。
检测中的取值
v.getScrollY()表示scrollview的上面滚出去的长度。
v.getHeight()表示scrollview本身看上去的高度。
view.getChildAt(0).getMeasuredHeight()是内容的高度。这里不能用view.getMeasureHeight()。
view.getMeasuredHeight() = 1038
v.getHeight() = 1038
v.getScrollY() = 2962
view.getChildAt(0).getMeasuredHeight() = 4000
1. 源起
很多分页加载的组件都是继承自ScrollView,分页要检查是否滚动到底部,下拉刷新需要检测是否滚动到顶部。
2. 代码
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <ScrollView android:id="@+id/scroll_view" android:layout_width="match_parent" android:layout_height="match_parent" android:scrollbars="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@android:color/darker_gray" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="2000dip" android:text="text" /> </LinearLayout> </ScrollView> </RelativeLayout>
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final ScrollView view = (ScrollView) findViewById(R.id.scroll_view); view.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: break; case MotionEvent.ACTION_MOVE: if (v.getScrollY() <= 0) { Log.d("scroll view", "top"); } else if (view.getChildAt(0).getMeasuredHeight() <= v.getHeight() + v.getScrollY()) { Log.d("scroll view", "bottom"); Log.d("scroll view", "view.getMeasuredHeight() = " + view.getMeasuredHeight() + ", v.getHeight() = " + v.getHeight() + ", v.getScrollY() = " + v.getScrollY() + ", view.getChildAt(0).getMeasuredHeight() = " + view.getChildAt(0).getMeasuredHeight()); } break; default: break; } return false; } }); } }
3. 说明
检测滚动的时机
上面是在ScrollView的onTouchListener里面检测,如果是继承ScrollView,也可以在onTouchEvent或者onInterceptTouchEvent中判断。
检测中的取值
v.getScrollY()表示scrollview的上面滚出去的长度。
v.getHeight()表示scrollview本身看上去的高度。
view.getChildAt(0).getMeasuredHeight()是内容的高度。这里不能用view.getMeasureHeight()。
view.getMeasuredHeight() = 1038
v.getHeight() = 1038
v.getScrollY() = 2962
view.getChildAt(0).getMeasuredHeight() = 4000
相关文章推荐
- [Android] ScrollView滚动到底部和顶部的检测
- Android中如何消除ScrollView滚动到顶部或底部时的边框
- Android ScrollView滚动条控件,滚动到底部或顶部
- 滚动到底部或顶部响应的ScrollView使用
- Android设置ScrollView滚动到底部和顶部
- ScrollView滚动到底部和顶部
- android scrollview 自动滚动到顶部或者底部
- 判断ScrollView 是否滚动到底部或顶部
- android scrollview 自动滚动到顶部或者底部的实例
- 判断ScrollView是否滚动到顶部或底部
- 消除ScrollView(Listview)滚动到顶部或底部时的边框
- Android 控制ScrollView滚动到底部或顶部
- Android ScrollView 页面滚动至顶部及底部的处理
- 滚动到底部或顶部响应的ScrollView使用
- Android 自定义ScrollView嵌套滚动兼容,监听滑动状态:顶部、底部、停止、滑动中
- android scrollview滚动到底部 顶部
- Android判断ListView滚动到最顶部第0条item完全完整可见及最底部最后一条item完全完整可见
- ScrollView嵌套ListView,GridView,ViewPager,以及这些控件自动滚动到底部问题的解决
- 关于ListView,GridView,ScrollView是否滑动到了顶部或者底部处理
- Android 禁止初始时ScrollView自动滚动到底部