android ScrollView滚动到底部和顶部的检测

u011859089 2013-08-29 06:03:09
1. 源起


很多分页加载的组件都是继承自ScrollView,分页要检查是否滚动到底部,下拉刷新需要检测是否滚动到顶部。
(PS:一个不错的Android学习交流群278744577,验证:cs,有兴趣的话可以加入进来一起讨论)
2. 代码
<SPAN style="FONT-FAMILY: Microsoft YaHei; FONT-SIZE: 18px"><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></SPAN>

<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>[java] view plaincopyprint?
<SPAN style="FONT-FAMILY: Microsoft YaHei; FONT-SIZE: 18px">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;
} });
}
}</SPAN>

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外面检测,使用onTouch的MotionEvent.ACTION_MOVE。如果是继承ScrollView,可以在其他回调的时候检测。

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
...全文
112 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
hesaiisme 2013-09-30
  • 打赏
  • 举报
回复
学习了,LZ很不错哦
Super ScrollView for UGUI提供基于UGUI ScrollRect的可轻松定制的ScrollView。它是一组C#脚本,可帮助您创建所需的ScrollView。这是非常强大的和高度优化的性能。 文件 Android演示应用程序 演示: - 聊天消息列表演示 - 水平画廊演示 - 垂直画廊演示 - GridView演示 - PageVew演示 - TreeVew演示 - 与稠粘头演示的TreeView - 旋转日期选择器 - 更改项目高度演示 - 下拉刷新演示 - 拉起来加载更多的演示 - 点击加载更多演示 - 选择并删除演示 - GridView删除项目演示 - 顶部底部的演示 - 自下而上的演示 - 从左到右的演示 - 右侧演示 - 响应GridView演示 - TreeViewWithChildrenIndent演示 特征: - ListView和GridView和TreeView - 无限的项目 - 项目在不同的大小(高度/宽度) - 具有不同预制的物品 - 在初始时间大小未知的项目 - 垂直滚动视图(从上到下,从下到上) - 水平滚动视图(从左到右,从右到左) - 项目填充 - 滚动到指定的项目 - 滚动到具有偏移量的项目 - 项目计数在运行时更改 - 项目大小(高度/宽度)在运行时更改 - 物品捕捉到视口中的任何位置 - 项目循环,如微调 - 添加/删除项目 - 全部删除/删除所有项目 - 刷新并重新加载项目 - 使用池缓存项目,不要在运行时销毁项目 - 有效回收物品 - 平台无关 - UGUI支持 - 支持Unity平台(IOS / Android / Mac / PC / Console / Winphone / WebGL ...)

80,359

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧