分享

实现ScrollView的分页处理

 點點滴滴 2012-05-26
0
本帖最后由 kfperfect 于 2012-3-1 13:56 编辑

项目中遇到了这样一种类似于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. }
复制代码
 import android.content.Context;
import android.util.AttributeSet;
import android.widget.ScrollView;

public class ControlableScrollView extends ScrollView {

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

        public ControlableScrollView(Context context) {
                super(context);
        }

        public ControlableScrollView(Context context, AttributeSet attrs) {
                super(context, attrs);
        }

        public ControlableScrollView(Context context, AttributeSet attrs,
                        int defStyle) {
                super(context, attrs, defStyle);
        }

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

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

        //自定义的监听接口,满足条件是调用其中的方法,执行相应的操作
        public static interface OnScrollListener {
                /**
                 * called when the view scrolled to the bottom edge.
                 *
                 * @param v
                 *            ControlableScrollView
                 */
                public void onScroll(ControlableScrollView v);
        }
}


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>
复制代码
<...ControlableScrollView
            android:id="@+id/log_scroll"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:layout_alignParentBottom="true"
            android:layout_below="@id/log_expanded_title" >

            <TextView
                android:id="@+id/log_text"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="13sp" />
        </...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.         }
复制代码
         private ControlableScrollView mScrollView;
@Override
        public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.g_transbomb_view);

        mScrollView = findViewById(R.id.log_scroll);

        mScrollView.setOnScrollListener(new OnScrollListener() {

                        @Override
                        public void onScroll(ControlableScrollView v) {
                                //执行到此处,说明ScrollView已经滑动到最后,可进行相应的处理。                        
                                        }
                                }
                        }
                });
        }




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

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多