自定义滑动到底部自动加载SwipeRefreshLayout

/**
 * 自定义滑动到底部自动加载SwipeRefreshLayout
 */
public class MySwipeRefreshLayout extends SwipeRefreshLayout implements AbsListView.OnScrollListener {

    private FrameLayout footerLayoutHolder;

    // 触发移动事件的最短距离                                                                                                                                 private int mTouchSlop;

    // listview实例
    private ListView mListView;

    //ListView的加载中footer
    private View mListViewFooter;

    // 按下时的y坐标
    private int mYDown;

    // 抬起时的y坐标, 与mYDown一起用于滑动到底部时判断是上拉还是下拉
    private int mLastY;

    // 是否在加载中 ( 上拉加载更多 )
    private boolean isLoading = false;

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

    public MySwipeRefreshLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        // 触发移动事件的最短距离
        mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
    }

    /**
     * 当加载完成xml后,就会执行
     */
    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        // 初始化ListView对象
        if (mListView == null) {
            getListView();
            this.setColorSchemeResources(android.R.color.holo_blue_light, android.R.color.holo_red_light,
            android.R.color.holo_orange_light, android.R.color.holo_green_light);
        }
    }

    /**
     * 获取ListView对象
     */
    private void getListView() {
        int childs = getChildCount();
        if (childs > 0) {
            View childView = getTargetView();
            if (childView instanceof ListView) {
                mListView = (ListView) childView;

                mListViewFooter = LayoutInflater.from(getContext()).inflate(R.layout.view_refresh_list_footer, null);
                // 以下三行代码是为了控制FooterView的可见状态,listView的BUG
                footerLayoutHolder = new FrameLayout(getContext());
                footerLayoutHolder.addView(mListViewFooter, 0, new FrameLayout.LayoutParams(                                                            FrameLayout.LayoutParams.MATCH_PARENT,FrameLayout.LayoutParams.WRAP_CONTENT));
                mListView.addFooterView(footerLayoutHolder);

                mListViewFooter.setVisibility(GONE);

                // 设置滚动监听器给ListView, 使得滚动的情况下也可以自动加载
                mListView.setOnScrollListener(this);
            }    
        }
    }

    /**
     * 获取目标控件
     */
    private AbsListView getTargetView() {
        for (int i = 0; i < getChildCount(); i++) {
            View child = getChildAt(i);
            if (child instanceof AbsListView && child instanceof ListView) {
                return (AbsListView) child;
            }
        }
        return null;
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        final int action = event.getAction();
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                mYDown = (int) event.getRawY();
            break;

            case MotionEvent.ACTION_MOVE:
                mLastY = (int) event.getRawY();
            break;

            case MotionEvent.ACTION_UP:
                if (canLoad()) {
                    loadData();
                }
            break;
            default:
            break;
        }
        return super.dispatchTouchEvent(event);
    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
                         int totalItemCount) {
    // 滚动时到了最底部也可以加载更多
        if (canLoad()) {
            loadData();
        }
    }

    /**
     * 是否可以加载更多, 条件是到了最底部, listview不在加载中, 且为上拉操作.
     */
    private boolean canLoad() {
        return isBottom() && !isLoading && isPullUp();
    }

    /**
     * 判断是否到了最底部
    */
    private boolean isBottom() {
        if (mListView != null && mListView.getAdapter() != null) {
            return mListView.getLastVisiblePosition() == (mListView.getAdapter().getCount() - 1);
        }
        return false;
    }

    /**
     * 是否是上拉操作
    */
    private boolean isPullUp() {
        return (mYDown - mLastY) >= mTouchSlop;
    }

    /**
     * 如果到了最底部,而且是上拉操作.那么执行onLoad方法
    */
    private void loadData() {
        if (mOnLoadListener != null) {
            // 设置状态
            setLoading(true);
            mOnLoadListener.onLoad();
        }
    }

    public void setLoading(boolean loading) {
        isLoading = loading;
        if (isLoading) {
            mListViewFooter.setVisibility(VISIBLE);
        } else {
            mListViewFooter.setVisibility(GONE);
        }
    }

    /**
     * 加载更多的监听器
     * 回调函数
     *
     * @author mrsimple
     */
    public interface OnLoadListener {
        void onLoad();
    }

    public void setOnLoadListener(OnLoadListener loadListener) {
        mOnLoadListener = loadListener;
    }

    private OnLoadListener mOnLoadListener;
}


view_refresh_list_footer.xml



    

    
    





你可能感兴趣的:(自定义滑动到底部自动加载SwipeRefreshLayout)