上拉加载,包裹任意布局,多层嵌套listview

  上拉加载是在原生控件swipeRefreshlayout的基础上增加的功能,实现方式网上copy就行。主要是refreshLayout包裹多层布局,多个listview的时候,操作的是那个listview。代码可以看出所有上拉加载的操作均在布局绘制完成,且数据加载完毕以后,能够对界面界面进行操作的时候,所以直接指定某个listview是可以的。 

public class RefreshLayout extends SwipeRefreshLayout implements AbsListView.OnScrollListener {
    private RefreshLayout.OnViewRefreshListener mListener;
    private RefreshLayout.OnListViewScrollListener mOnListViewScrollListener;
    private RefreshLayout.OnListViewScrollStateChanged mOnListViewScrollStateChanged;
    private int mTouchSlop;
    private float startY;
    private float startX;
    private boolean mIsChildDragging;
    private LJRefreshListView mLJRefreshListView;
    private View mFootContainerView;
    private View mFootLoadEnd;
    private TextView mFootLoadEndText;
    private int mYDown;
    private int mLastY;
    private boolean mIsLoading;
    private boolean mHasMore;

    public RefreshLayout(Context context) {
        this(context, (AttributeSet)null);
    }

    public RefreshLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mIsLoading = false;
        this.mHasMore = false;
        this.mFootContainerView = (LinearLayout) LayoutInflater.from(context).inflate(layout.baseview_view_refresh_bottom, (ViewGroup)null);
        this.mFootLoadEnd = (LinearLayout)LayoutInflater.from(context).inflate(layout.baseview_view_refresh_bottom_end, (ViewGroup)null);
        this.mFootLoadEndText = (TextView)this.mFootLoadEnd.findViewById(id.view_load_end_text);
        this.mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
    }

    public void setLJRefreshListView(LJRefreshListView LJRefreshListView) {
        mLJRefreshListView = LJRefreshListView;
    }

    public void setRefreshingNoLoading() {
        this.setProgressViewOffset(false, 0, (int)(-60.0F * this.getResources().getDisplayMetrics().density));
        this.setRefreshing(true);
        this.post(new Runnable() {
            public void run() {
                RefreshLayout.this.setProgressViewOffset(false, (int)(-60.0F * RefreshLayout.this.getResources().getDisplayMetrics().density), (int)(64.0F * RefreshLayout.this.getResources().getDisplayMetrics().density));
            }
        });
    }

    public void setRefreshing(boolean refresh) {
        super.setRefreshing(refresh);
        if (refresh) {
            this.mIsLoading = true;
            if (this.mListener != null) {
                this.mListener.onRefresh(false);
            }
        } else {
            this.mIsLoading = false;
        }

    }

    public void setOnRefreshListener(RefreshLayout.OnViewRefreshListener listener) {
        this.mListener = listener;
        this.setOnRefreshListener(new OnRefreshListener() {
            public void onRefresh() {
                if (RefreshLayout.this.mListener != null) {
                    RefreshLayout.this.mListener.onRefresh(false);
                }

            }
        });
    }

    public void setOnListViewScrollListener(RefreshLayout.OnListViewScrollListener listener) {
        this.mOnListViewScrollListener = listener;
    }

    public void setOnListViewScrollStateChanged(RefreshLayout.OnListViewScrollStateChanged listener) {
        this.mOnListViewScrollStateChanged = listener;
    }

    /** @deprecated */
    @Deprecated
    public void setOnRefreshListener(OnRefreshListener listener) {
        super.setOnRefreshListener(listener);
    }

    public void setLoadingMoreEnabled(boolean enable) {
        if (this.mLJRefreshListView != null) {
            this.mHasMore = enable;
            this.mLJRefreshListView.removeFooterView(this.mFootContainerView);
            this.mLJRefreshListView.removeFooterView(this.mFootLoadEnd);
        }

    }

    public void setLoadingMoreEnabled(boolean enable, String loadFinishMsg) {
        if (this.mLJRefreshListView != null) {
            this.mHasMore = enable;
            this.mLJRefreshListView.removeFooterView(this.mFootContainerView);
            this.mLJRefreshListView.removeFooterView(this.mFootLoadEnd);
            if (!this.mHasMore && !TextUtils.isEmpty(loadFinishMsg)) {
                this.mLJRefreshListView.addFooterView(this.mFootLoadEnd);
                this.mFootLoadEndText.setText(loadFinishMsg);
            }
        }

    }

    public boolean dispatchTouchEvent(MotionEvent event) {
        int action = event.getAction();
        switch(action) {
            case 0:
                this.mYDown = (int)event.getRawY();
                break;
            case 1:
                if (this.canLoadMore()) {
                    this.loadMore();
                }
                break;
            case 2:
                this.mLastY = (int)event.getRawY();
        }

        return super.dispatchTouchEvent(event);
    }

    private boolean canLoadMore() {
        return this.isBottom() && !this.mIsLoading && this.isPullUp() && this.mHasMore;
    }

    private boolean isBottom() {
        if (this.mLJRefreshListView != null && this.mLJRefreshListView.getAdapter() != null) {
            return this.mLJRefreshListView.getLastVisiblePosition() == this.mLJRefreshListView.getAdapter().getCount() - 1;
        } else {
            return false;
        }
    }

    private boolean isPullUp() {
        return this.mYDown - this.mLastY >= this.mTouchSlop;
    }

    private void loadMore() {
        if (this.mListener != null) {
            this.setLoading(true);
            this.mListener.onRefresh(true);
        }

    }

    private void setLoading(boolean loading) {
        this.mIsLoading = loading;
        if (this.mIsLoading) {
            this.mLJRefreshListView.removeFooterView(this.mFootContainerView);
            this.mLJRefreshListView.addFooterView(this.mFootContainerView);
        } else {
            this.mYDown = 0;
            this.mLastY = 0;
        }

    }

    public void onScrollStateChanged(AbsListView view, int scrollState) {
        if (this.mOnListViewScrollStateChanged != null) {
            this.mOnListViewScrollStateChanged.onScrollStateChanged(view, scrollState);
        }

    }

    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        if (this.mOnListViewScrollListener != null) {
            this.mOnListViewScrollListener.onScroll(view, firstVisibleItem, visibleItemCount, totalItemCount);
        }

        if (this.canLoadMore()) {
            this.loadMore();
        }

    }

    public void setVisibility(int visibility) {
        if (this.mLJRefreshListView != null) {
            this.mLJRefreshListView.setVisibility(visibility);
        }

        super.setVisibility(visibility);
    }

    public boolean onInterceptTouchEvent(MotionEvent ev) {
        int action = ev.getAction();
        switch(action) {
            case 0:
                this.startY = ev.getY();
                this.startX = ev.getX();
                this.mIsChildDragging = false;
                break;
            case 1:
            case 3:
                this.mIsChildDragging = false;
                break;
            case 2:
                if (this.mIsChildDragging) {
                    return false;
                }

                float endY = ev.getY();
                float endX = ev.getX();
                float distanceX = Math.abs(endX - this.startX);
                float distanceY = Math.abs(endY - this.startY);
                if (distanceX > (float)this.mTouchSlop && distanceX > distanceY) {
                    this.mIsChildDragging = true;
                    return false;
                }
        }

        return super.onInterceptTouchEvent(ev);
    }

    public interface OnListViewScrollStateChanged {
        void onScrollStateChanged(AbsListView var1, int var2);
    }

    public interface OnListViewScrollListener {
        void onScroll(AbsListView var1, int var2, int var3, int var4);
    }

    public interface OnViewRefreshListener {
        void onRefresh(boolean var1);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值