ScrollView嵌套ListView分页加载

ScrollView嵌套ListView分页加载数据

在ListView的onMeasure重写之后,通常所用的setOnScrollListener失去作用,无法监听ListView的滚动事件。
本例通过监听ScrollView滚动到底部,来实现分页加载。

package net.xxx.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.ScrollView;

/**
 * Created by max on 2016/10/26.
 */
public class MyScrollView extends ScrollView {

    private static final String TAG = MyScrollView.class.getSimpleName();


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

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

    public MyScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);

//        Log.d(TAG, "onScrollChanged: 竖直高度t = " + t);
//        Log.d(TAG, "onScrollChanged: 竖直高度oldt = " + oldt);

        //将高度传出去
        if (mOnScrollHeightListener != null) {
            mOnScrollHeightListener.onScrollHeight(t);
        }
    }


//滚动到底部时,clampedY变为true,其余情况为false,通过回调将状态传出去即可。
    @Override
    protected void onOverScrolled(int scrollX, int scrollY, boolean clampedX, boolean clampedY) {
        super.onOverScrolled(scrollX, scrollY, clampedX, clampedY);
        if (scrollY != 0 && null != mOnScrollToBottomListener) {
            mOnScrollToBottomListener.onScrollBottomListener(clampedY);
        }
    }

    private OnScrollToBottomListener mOnScrollToBottomListener;

    public void setOnScrollToBottomLintener(OnScrollToBottomListener listener) {
        mOnScrollToBottomListener = listener;
    }

    public interface OnScrollToBottomListener {
        public void onScrollBottomListener(boolean isBottom);
    }

//复杂实现方式,将滚动距离传出去,获取滚动的位置,来判断也可。
    public interface OnScrollHeightListener {
        void onScrollHeight(int scrollY);
    }

    private OnScrollHeightListener mOnScrollHeightListener;

    public void setOnScrollHeightListener(OnScrollHeightListener onScrollHeightListener) {
        mOnScrollHeightListener = onScrollHeightListener;
    }
    //    public void
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值