LoadMoreSwipeRefreshLayout

封装带有上拉加载的控件

package com.handongkeji.customview;

import android.content.Context;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewConfiguration;
import android.widget.AbsListView;
import android.widget.ListAdapter;
import android.widget.ListView;

import com.handongkeji.business.ordersearch.adapter.OrderSearchFragmentListAdapter;
import com.handongkeji.jeno.R;


/**
 * create by  宋佳  on 2017/4/27 9:19 .
 * 描述 : 可以下拉加载更多的 SwipeRefreshLayout
 */

public class LoadMoreSwipeRefreshLayout extends SwipeRefreshLayout {
    private final int mScaledTouchSlop;
    private final View mFooterView;
    private ListView mListView;
    private OnLoadListener mOnLoadListener;

    /**
     * 正在加载状态
     */
    private boolean isLoading;

    public LoadMoreSwipeRefreshLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        // 填充底部加载布局
        mFooterView = View.inflate(context, R.layout.view_footer, null);

        // 表示控件移动的最小距离,手移动的距离大于这个距离才能拖动控件
        mScaledTouchSlop = 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);
        // 获取ListView,设置ListView的布局位置

        if (mListView == null) {
            // 判断容器有多少个孩子
            if (getChildCount() > 0) {
                // 判断第一个孩子是不是ListView
                if (getChildAt(0) instanceof ListView) {
                    // 创建ListView对象
                    mListView = (ListView) getChildAt(0);
                    ListAdapter adapter = mListView.getAdapter();
                    if (adapter instanceof OrderSearchFragmentListAdapter) {
                        Log.i("和数据", "onLayout: " + "OrderSearchFragmentListAdapter");
                    }
                    setScrollView(adapter);


                    // 设置ListView的滑动监听
//                    setListViewOnScroll();
                }
            }
        }
    }

    private int visibleLastIndex;

    private void setScrollView(final ListAdapter adapter) {
        mListView.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView absListView, int scrollState) {
                int itemsLastIndex = adapter.getCount() - 1;    //数据集最后一项的索引
                int lastIndex = itemsLastIndex;             //加上底部的loadMoreView项
                if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE && visibleLastIndex == lastIndex) {
                    //如果是自动加载,可以在这里放置异步加载数据的代码
                    loadData(adapter);
                }
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                visibleLastIndex = firstVisibleItem + visibleItemCount - 1;
            }
        });
    }


    /**
     * 处理加载数据的逻辑
     */
    private void loadData(ListAdapter adapter) {
        if (mOnLoadListener != null) {
            // 设置加载状态,让布局显示出来
            setLoading(adapter, true);
            mOnLoadListener.onLoad();
        }

    }

    /**
     * 设置加载状态,是否加载传入boolean值进行判断
     *
     * @param loading
     */
    public void setLoading(ListAdapter adapter, boolean loading) {
        // 修改当前的状态
        isLoading = loading;
        if (isLoading) {
            // 显示布局  当 listview中的数据小于 5条是不进行显示
            if (mListView != null && mListView.getLastVisiblePosition() > 5 && mFooterView != null)
                mListView.addFooterView(mFooterView);
            if (adapter != null && mListView.getLastVisiblePosition() > 10)
                mListView.setSelection(adapter.getCount() - 1);
        } else {
            // 隐藏布局
            if (mListView != null && mFooterView != null)
                mListView.removeFooterView(mFooterView);
        }
    }


    /**
     * 上拉加载的接口回调
     */

    public interface OnLoadListener {
        void onLoad();
    }

    public void setOnLoadListener(OnLoadListener listener) {
        this.mOnLoadListener = listener;
    }
}

xml中引用

LoadMoreSwipeRefreshLayout_第1张图片
Paste_Image.png
srefresh_orederSearch.setOnLoadListener(this);
   /**
     * 加载更多
     */
    @Override
    public void onLoad() {
      ......
    }

你可能感兴趣的:(LoadMoreSwipeRefreshLayout)