首页 > 代码库 > 自己封装的工具类,使用原生SwipeRefreshLayout+RecycleView实现下拉刷新和加载更多

自己封装的工具类,使用原生SwipeRefreshLayout+RecycleView实现下拉刷新和加载更多

实现SwipeRefreshLayout+RecycleView实现刷新

在你的xml文件里写上如下代码:

<android.support.v4.widget.SwipeRefreshLayout            android:id="@+id/SwipeRefreshLayout"            android:layout_width="match_parent"            android:layout_height="match_parent">            <android.support.v7.widget.RecyclerView                android:id="@+id/recyclerView"                android:layout_width="match_parent"                android:layout_height="match_parent"                android:cacheColorHint="#00000000"                android:overScrollMode="never"                android:scrollbars="none"/>        </android.support.v4.widget.SwipeRefreshLayout>

在你的Activity或Fragment实现RecyclerViewLoadMoreUtil.RefreshDataListener刷新接口并调用以下代码初始化:

这里的init()方法中的
参数一:content
参数二:SwipeRefreshLayout控件
参数三:RecyclerView控件
参数四:继承RecyclerView.Adapter的adapter,这里可以使用我另外写的一篇关于adapter的封装 
参数五:刷新接口,这里如果在Activity或Fragment实现RecyclerViewLoadMoreUtil.RefreshDataListener就可以在此处填this了

recyclerViewLoadMoreUtil = new RecyclerViewLoadMoreUtil();        recyclerViewLoadMoreUtil.init(mContext, mSwipeRefreshLayout, mRecyclerView, mAdapter, this);        recyclerViewLoadMoreUtil.setColorSchemeResources(R.color.yellow_press, R.color.green, R.color.red);        recyclerViewLoadMoreUtil.autoRefreshing();//第一次自动加载一次        .... @Override    public void onRefresh() {        recyclerViewLoadMoreUtil.endRefreshing();        //Do the things you want to do    }     @Override    public boolean loadMore() {        recyclerViewLoadMoreUtil.endLoading();        //Do the things you want to do        return true;    }

以下是该刷新工具类的全部代码

/** * Created by xiongxuesong-pc on 2016/6/17. * 配合原生RecyclerVew和SwipeRefreshLayout加载更多 */public class RecyclerViewLoadMoreUtil {    protected Context mContext;    protected SwipeRefreshLayout mSwipeRefreshLayout;    protected RecyclerView mRecyclerView;    protected RecyclerView.Adapter mAdapter;    protected RefreshDataListener mRefreshDataListener;    private boolean mIsRefresh = true;//是否禁止下拉刷新,默认是不禁止下拉刷新    private boolean mIsLoadMore = true;//是否禁止上拉刷新,默认是不禁止上拉刷新    private boolean isSlidingToScreenBottom = false;//用来标记是否正在向最后一个滑动,既是否向右滑动或向下滑动    public RecyclerViewLoadMoreUtil() {    }    public void init(final Context context, final SwipeRefreshLayout swipeRefreshLayout, RecyclerView recyclerView, final RecyclerView.Adapter adapter, final RefreshDataListener refreshDataListener) {        initConfig(context, swipeRefreshLayout, recyclerView, adapter, refreshDataListener);        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {            @Override            public void onRefresh() {                boolean isRefreshing = swipeRefreshLayout.isRefreshing();                if (isRefreshing) {                    refreshDataListener.onRefresh();                }            }        });        final LinearLayoutManager layoutManager = new LinearLayoutManager(context);        recyclerView.setLayoutManager(layoutManager);        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {            @Override            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {                LinearLayoutManager manager = (LinearLayoutManager) recyclerView.getLayoutManager();                // 当不滚动时                boolean idleFlag = newState == RecyclerView.SCROLL_STATE_IDLE;                if (idleFlag) {                    //获取最后一个完全显示的ItemPosition                    int lastVisibleItem = manager.findLastCompletelyVisibleItemPosition();                    int totalItemCount = mAdapter.getItemCount();                    // 判断是否滚动到屏幕底部,并且是向右滚动                    if (lastVisibleItem == (totalItemCount - 1) && isSlidingToScreenBottom) {                        if (mScrolledBottomListener != null) {                            mScrolledBottomListener.ScrolledBottom(recyclerView);                        }                        if (mIsLoadMore) {//可以加载更多                            mSwipeRefreshLayout.setEnabled(false);//禁止使用下拉刷新                            mRefreshDataListener.loadMore();                            Log.d("TAG", "howes right=" + manager.findLastCompletelyVisibleItemPosition());//                                Toast.makeText(mContext, "加载更多", Toast.LENGTH_SHORT).show();                        } else {//                            Toast.makeText(mContext, "不能加载更多", Toast.LENGTH_SHORT).show();                        }                    }                }            }            @Override            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {                super.onScrolled(recyclerView, dx, dy);                Log.d("TAG", "dy=" + dy);                //dx用来判断横向滑动方向,dy用来判断纵向滑动方向                //当列表项的总高度小于屏幕,即不会滚动列表时,dx、dy都等于0                if (dy > 0) {                    //大于0表示,正在向右滚动,向上滚动                    isSlidingToScreenBottom = true;                } else {                    //小于等于0 表示停止或向左滚动,向下滚动                    isSlidingToScreenBottom = false;                }                if (mScrollingListener != null) {                    mScrollingListener.scroll(isSlidingToScreenBottom);                }            }        });    }    private void initConfig(Context context, SwipeRefreshLayout swipeRefreshLayout, RecyclerView recyclerView, RecyclerView.Adapter adapter, RefreshDataListener refreshDataListener) {        this.mContext = context;        this.mSwipeRefreshLayout = swipeRefreshLayout;        this.mRecyclerView = recyclerView;        this.mAdapter = adapter;        this.mRefreshDataListener = refreshDataListener;    }    /**     * 首次进入页面可以自动加载,要使用必须在{@link #init(Context, SwipeRefreshLayout, RecyclerView, RecyclerView.Adapter, RefreshDataListener)}     * 之后调用     */    public void autoRefreshing() {        mSwipeRefreshLayout.post(new Runnable() {            @Override            public void run() {                mSwipeRefreshLayout.setRefreshing(true);                mRefreshDataListener.onRefresh();            }        });    }    /**     * 要使用必须在{@link #init(Context, SwipeRefreshLayout, RecyclerView, RecyclerView.Adapter, RefreshDataListener)}     * 之后调用     *     * @param colorResIds     */    public void setColorSchemeResources(@ColorRes int... colorResIds) {        mSwipeRefreshLayout.setColorSchemeResources(colorResIds);    }    public void endRefreshing() {        mSwipeRefreshLayout.setRefreshing(false);    }    public void endLoading() {        if (mIsRefresh) {//如果之前已经设置过禁止下拉刷新,此时不应该在设置成可下拉刷新            setPullDownRefreshEnable(true);//可以使用下拉刷新        } else {//如果之前已经设置过禁止下拉刷新,此时不应该在设置成可下拉刷新            setPullDownRefreshEnable(false);//不可以使用下拉刷新        }    }    /**     * 设置列表是否禁止下拉刷新,是否显示下拉刷新动画只能通过该方法设置     *     * @param isRefresh true 可以使用下拉刷新;false 不可以使用下拉刷新     */    public void setPullDownRefreshEnable(boolean isRefresh) {        mIsRefresh = isRefresh;        mSwipeRefreshLayout.setEnabled(isRefresh);    }    /**     * 设置列表是否禁止上拉刷新     *     * @param isLoadMore true 可以使用上拉刷新;false 不可以使用上拉刷新     */    public void setPullUpRefreshEnable(boolean isLoadMore) {        mIsLoadMore = isLoadMore;    }    public interface RefreshDataListener {        void onRefresh();        boolean loadMore();    }    private ScrollingListener mScrollingListener;    public interface ScrollingListener {        /**         * true 向上滚动;false 向下滚动         *         * @param scrollState         */        void scroll(boolean scrollState);    }    public void setScrollingListener(ScrollingListener listener) {        mScrollingListener = listener;    }    private ScrolledBottomListener mScrolledBottomListener;    public interface ScrolledBottomListener {        /**         * 列表滚动到屏幕底部时调用         *         * @param recyclerView         */        void ScrolledBottom(RecyclerView recyclerView);    }    public void setScrollingListener(ScrolledBottomListener listener) {        mScrolledBottomListener = listener;    }}

这是本人从项目中抽取出来的,暂时没在项目中出现问题,希望能帮助到你。

转载请注明出处,谢谢!

自己封装的工具类,使用原生SwipeRefreshLayout+RecycleView实现下拉刷新和加载更多