实现ScrollView的分页处理

本文介绍了一种通过实现自定义ScrollView来解决ListView分页显示需求的方法,包括如何在ScrollView中添加监听器,以及如何在滑动到底部时触发加载下一页的数据。通过这种方式,可以在不依赖于ListView提供的方法的情况下实现类似的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

项目中遇到了这样一种类似于ListView的需求:使用ScrollView分页显示数据,滑动到最后的时候自动加载下一页。如果是ListView我们可以判断当前显示的last item,从而得知是否已经滑动到最后,而ScrollView并没有提供这样一种方法,甚至不能添加OnScrollListener。那么我们该如何处理呢?综合参考了网友们的方法,我总结除了如下的实现形式:
1、实现自己的ScrollView
  1. import android.content.Context;
  2. import android.util.AttributeSet;
  3. import android.widget.ScrollView;

  4. public class ControlableScrollView extends ScrollView {

  5.         //自定义的监听器,当满足条件时调用
  6.         private OnScrollListener mListener;

  7.         public ControlableScrollView(Context context) {
  8.                 super(context);
  9.         }

  10.         public ControlableScrollView(Context context, AttributeSet attrs) {
  11.                 super(context, attrs);
  12.         }

  13.         public ControlableScrollView(Context context, AttributeSet attrs,
  14.                         int defStyle) {
  15.                 super(context, attrs, defStyle);
  16.         }

  17.         //覆盖父类的方法,当scroll时调用,可判断是否已经滑到最后,computeVerticalScrollRange方法用于获取ScrollView的总高度
  18.         @Override
  19.         protected void onScrollChanged(int l, int t, int oldl, int oldt) {
  20.                 super.onScrollChanged(l, t, oldl, oldt);
  21.                 if (mListener != null
  22.                                 && getHeight() + getScrollY() >= computeVerticalScrollRange()) {
  23.                         mListener.onScroll(this);
  24.                 }
  25.         }

  26.         //添加监听
  27.         public void setOnScrollListener(OnScrollListener onScrollListener) {
  28.                 this.mListener = onScrollListener;
  29.         }

  30.         //自定义的监听接口,满足条件是调用其中的方法,执行相应的操作
  31.         public static interface OnScrollListener {
  32.                 /**
  33.                  * called when the view scrolled to the bottom edge.
  34.                  *
  35.                  * @param v
  36.                  *            ControlableScrollView
  37.                  */
  38.                 public void onScroll(ControlableScrollView v);
  39.         }
  40. }
复制代码

2、在布局文件中引用自定义的ScrollView
我的ScrollView中添加了一个TextView用来显示字符串。代码终身略号代表包路径。

  1. <...ControlableScrollView
  2.             android:id="@+id/log_scroll"
  3.             android:layout_width="match_parent"
  4.             android:layout_height="200dp"
  5.             android:layout_alignParentBottom="true"
  6.             android:layout_below="@id/log_expanded_title" >

  7.             <TextView
  8.                 android:id="@+id/log_text"
  9.                 android:layout_width="match_parent"
  10.                 android:layout_height="wrap_content"
  11.                 android:textSize="13sp" />
  12.         </...ControlableScrollView>
复制代码
3、在activity中进行相应处理,主要就是分页加载的处理。此处未列出完整文件,只展示了使用自定义ScrollView的代码。
  1.         private ControlableScrollView mScrollView;
  2. @Override
  3.         public void onCreate(Bundle savedInstanceState) {
  4.         super.onCreate(savedInstanceState);
  5.         setContentView(R.layout.g_transbomb_view);

  6.         mScrollView = findViewById(R.id.log_scroll);

  7.         mScrollView.setOnScrollListener(new OnScrollListener() {

  8.                         @Override
  9.                         public void onScroll(ControlableScrollView v) {
  10.                                 //执行到此处,说明ScrollView已经滑动到最后,可进行相应的处理。                        
  11.                                         }
  12.                                 }
  13.                         }
  14.                 });
  15.         }
复制代码


参考网址:
http://www.aoandroid.com/node/9101
http://www.iteye.com/problems/71100

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值