学习编程 2018-03-22
<?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/ptr_layout" android:layout_width="match_parent" android:layout_height="match_parent"> <FrameLayout android:layout_width="match_parent" android:layout_height="match_parent"> <!--RecyclerView控件,支持添加头部和底部view--> <android.support.v7.widget.RecyclerView android:id="@android:id/list" android:layout_width="match_parent" android:layout_height="match_parent" android:scrollbars="vertical|horizontal" android:clickable="true" android:focusable="true" /> <!--加载数据为空时的布局--> <FrameLayout android:id="@+id/empty" android:layout_width="match_parent" android:layout_height="match_parent" android:clickable="true" android:focusable="true"/> <!--正在加载数据中的布局--> <FrameLayout android:id="@+id/progress" android:layout_width="match_parent" android:layout_height="match_parent" android:clickable="true" android:focusable="true"/> <!--加载错误时的布局:网络错误或者请求数据错误--> <FrameLayout android:id="@+id/error" android:layout_width="match_parent" android:layout_height="match_parent" android:clickable="true" android:focusable="true"/> </FrameLayout> </android.support.v4.widget.SwipeRefreshLayout>
recyclerView.setLayoutManager(new FullyGridLayoutManager(activity, 3)); adapter = new DouBookAdapter(activity); recyclerView.setAdapter(adapter); //加载更多 adapter.setMore(R.layout.view_recycle_more, new RecyclerArrayAdapter.OnMoreListener() { @Override public void onMoreShow() { if (NetworkUtils.isConnected()) { if (adapter.getAllData().size() > 0) { getTopMovieData(mType, adapter.getAllData().size(), adapter.getAllData().size() + 21); } else { adapter.pauseMore(); } } else { adapter.pauseMore(); Toast.makeText(activity, "网络不可用", Toast.LENGTH_SHORT).show(); } } @Override public void onMoreClick() { } }); //设置没有数据 adapter.setNoMore(R.layout.view_recycle_no_more, new RecyclerArrayAdapter.OnNoMoreListener() { @Override public void onNoMoreShow() { if (NetworkUtils.isConnected()) { adapter.resumeMore(); } else { Toast.makeText(activity, "网络不可用", Toast.LENGTH_SHORT).show(); } } @Override public void onNoMoreClick() { if (NetworkUtils.isConnected()) { adapter.resumeMore(); } else { Toast.makeText(activity, "网络不可用", Toast.LENGTH_SHORT).show(); } } }); //设置错误 adapter.setError(R.layout.view_recycle_error, new RecyclerArrayAdapter.OnErrorListener() { @Override public void onErrorShow() { adapter.resumeMore(); } @Override public void onErrorClick() { adapter.resumeMore(); } }); //刷新 recyclerView.setRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { @Override public void onRefresh() { if (NetworkUtils.isConnected()) { getTopMovieData(mType , 0 , 30); } else { recyclerView.setRefreshing(false); Toast.makeText(activity, "网络不可用", Toast.LENGTH_SHORT).show(); } } });