网络请求框架(二)-实现刷新和加载更多操作

来源:互联网 发布:淘宝武汉钓鱼数码诈骗 编辑:程序博客网 时间:2024/04/29 14:52

网络请求框架-基于Volley(一):http://blog.csdn.net/hongxue8888/article/details/53506964
之前的文章简单对volley进行了扩展。

在大部分的app中总会存在一些页面展示的数据为List类型,并且存在下拉刷新和上拉加载更多操作。所以我们可以在这些页面中使用RecyclerView 和 SwipeRefreshLayout。

加载更多时这里使用mugen这个库。
看文章:http://blog.csdn.net/hongxue8888/article/details/53763958

首先布局文件:

<?xml version="1.0" encoding="utf-8"?><merge xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent">    <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.support.v4.widget.SwipeRefreshLayout>    <ViewStub        android:id="@+id/loading_vs"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:layout_gravity="center"        android:layout="@layout/loading" />    <ViewStub        android:id="@+id/empty_vs"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:layout="@layout/empty" />    <ViewStub        android:id="@+id/error_vs"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:layout_gravity="center"        android:layout="@layout/error" /></merge>

从上面的布局文件可以看出,网络请求有三个状态,加载中(loading)、无数据(empty)、请求错误(error),我们可以分别定义三个布局,用于不同状态下的展示。

将recyclerView连接上Mugen:

    private void initRecyclerView() {        //mCollectionView can be a ListView, GridView, RecyclerView or any instance of AbsListView!        attacher = Mugen.with(recyclerView, new MugenCallbacks() {            @Override            public void onLoadMore() {                setLoadingMore(true);//去告诉adapter可以显示加载更多了                onLoadingListener.onLoadMore();            }            @Override            public boolean isLoading() {                /* Return true if a load operation is ongoing. This will                * be used as an optimization to prevent further triggers                * if the user scrolls up and scrolls back down before                * the load operation finished.                *                * If there is no load operation ongoing, return false                */                return isLoadingMore();            }            @Override            public boolean hasLoadedAllItems() {                /*                * If every item has been loaded from the data store, i.e., no more items are                * left to fetched, you can start returning true here to prevent any more                * triggers of the load more method as a form of optimization.                *                * This is useful when say, the data is being fetched from the network                */                return onLoadingListener == null || (mode != Mode.BOTH && mode != Mode.LOAD_MORE);            }        }).start();        /* Use this to dynamically turn infinite scroll on or off. It is enabled by default */        attacher.setLoadMoreEnabled(true);        /* Use this to change when the onLoadMore() function is called.        * By default, it is called when the scroll reaches 2 items from the bottom */        attacher.setLoadMoreOffset(1);    }

接口:

public interface OnLoadingListener {    public void onRefresh();    public void onLoadMore();}

在实现该接口的地方覆写onLoadMore方法:

  @Override    public void onLoadMore() {        loadNextPage();    }

setLoadingMore(true)告诉Adapter显示加载更多。

 public void setLoadingMore(boolean loadingMore) {        this.loadingMore = loadingMore;        getAdapter().showLoading(loadingMore);    }
   public void showLoading(boolean loading) {        if (loading == this.loading)            return;        this.loading = loading;        //notifyDataSetChanged();        if (loading) {            notifyItemInserted(getItemCount());        } else {            notifyItemRemoved(getItemCount() + 1);        }    }
   @Override    public int getItemCount() {        return getCount() + (loading ? 1 : 0);    }
  @Override    public int getItemViewType(int position) {        if (loading && position >= getItemCount() - 1) {            return TYPE_LOADER;        }        return TYPE_ITEM;    }
    @Override    final public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {        if (viewType == TYPE_LOADER) {        //load_more是加载更多的布局            return new LoadMoreViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.load_more, parent, false));        }         //正常item布局在onCreateViewHolder2中进行        return onCreateViewHolder2(parent, viewType);    }

以上实现使用SwipeRefreshLayout、RecyclerView和Mugen实现了下拉刷新和上拉加载更多。

0 0
原创粉丝点击