1.加入相应的dependence依赖:
dependencies {
compile 'com.cjj.materialrefeshlayout:library:1.3.0'
}
2.materialRefreshLayout布局 包裹RecyclerView
<com.cjj.MaterialRefreshLayout
android:id="@+id/material_refresh_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
app:overlay="true"
app:wave_show="true"
app:wave_color="#90ffffff"
app:progress_colors="@array/material_colors"
app:wave_height_type="higher"
>
<android.support.v7.widget.RecyclerView
android:id="@+id/care_recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v7.widget.RecyclerView>
</com.cjj.MaterialRefreshLayout>
3.然后实例化materialRefreshLayout对象
private MaterialRefreshLayout materialRefreshLayout;
materialRefreshLayout = (MaterialRefreshLayout) view.findViewById(R.id.material_refresh_view);
4.初始化materialRefreshLayout 及其 下滑刷新的 和上滑加载的 具体 操作 的监听器
/**
* 初始化materialRefreshLayout 及其 下滑刷新的 和上滑加载的 具体 操作 的监听器
*
* @param view
*/
private void initRefreshLayout(final View view) {
materialRefreshLayout.setLoadMore(true); //设置是否价值啊更多
materialRefreshLayout.setMaterialRefreshListener(new MaterialRefreshListener() {
@Override
public void onRefresh(MaterialRefreshLayout materialRefreshLayout) {
refreshData(view);
}
@Override
public void onRefreshLoadMore(MaterialRefreshLayout materialRefreshLayout) {
super.onRefreshLoadMore(materialRefreshLayout);
loadData(view);
}
});
}
5.对上拉下拉操作refreshData(view); loadData(view);
//下拉刷新控件,刷新出数据
private void refreshData(View view) {
if (currPage < carePageBean.getTotalPage()) {
currPage = currPage + 1;
state = STATE_DO_REFREH;
initViewBean(view);
} else {
Toast.makeText(getContext(), "不能加载更多", Toast.LENGTH_SHORT).show();
materialRefreshLayout.finishRefresh();
}
}
//上拉刷新控件,属性出数据
private void loadData(View view) {
if (currPage > 0) {
currPage = currPage - 1;
state = STATE_NEED_MORE;
initViewBean(view);
} else {
Toast.makeText(getContext(), "不能加载更多", Toast.LENGTH_SHORT).show();
materialRefreshLayout.finishRefreshLoadMore();
}
}
6.进行RecycleView初始化操作数据获取啊配置什么的
/**
* 初始化ViewBean
*
* @param view
*/
private void initViewBean(final View view) {
httpHelper.get(MyUrl.Content.getCareInfoUrl(currPage,pageSize), new SpotsCallBack<CarePageBean<CareFirstViewBean>>(getContext()) {
@Override
public void onSuccess(Response response, CarePageBean<CareFirstViewBean> newCarePageBean) {
//初始化recycleList
carePageBean = newCarePageBean;
initRecycleList(newCarePageBean.getList());
//初始化RecyclerView
initRecyclerView(view);
}
@Override
public void onError(Response response, int code, Exception e) {
}
});
}