SwipeRefreshLayout
SwipeRefreshLayout是由google官方提出的下拉刷新空间, 在android.support.v4兼容库中.
使用
布局文件
SwipeRefreshLayout基本上可以包裹任何可以滚动的内容(ListView, RecyclerView…,WebView)
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| <?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/swipe_refresh" android:layout_width="match_parent" android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView android:id="@+id/news_rv" android:layout_width="match_parent" android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView> </android.support.v4.widget.SwipeRefreshLayout>
|
刷新操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| mSwipeRefreshLayout=(SwipeRefreshLayout)findViewById(R.id.swipe_refresh); mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { @Override public void onRefresh() { pageNum+=1; updateDatas(service,apiKey,pageNum); } });
.... public void updateDatas(Api.NewsService service,String apiKey,int pageNum){ service.getList(apiKey,pageNum) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Subscriber<News>() { @Override public void onCompleted() { ... }
@Override public void onError(Throwable e) { ... }
@Override public void onNext(News news) { datas=news.getDatas(); notifyDatasetChanged(datas); mSwipeRefreshLayout.setRefreshing(false); } }); }
|
在获取完数据之后, 需要调用mSwipeRefreshLayout.setRefreshing(false);, 否则加载的小圆圈将一直在旋转.
自定义
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| mSwipeRefreshLayout.setProgressViewOffset(true,R.attr.actionBarSize+20,200);
mySwipeRefreshLayout.setSize(SwipeRefreshLayout.LARGE);
mySwipeRefreshLayout.setEnabled(false);
mySwipeRefreshLayout.setProgressBackgroundColor(R.color.red);
mySwipeRefreshLayout.setColorSchemeResources(color1,color2);
|
Ultra Pull to Refresh
使用
配置
1 2 3 4 5 6 7 8 9 10 11
| build.gradle(project)
buildscript { repositories { jcenter() maven { url 'https://oss.sonatype.org/content/repositories/snapshots' } } ... }
|
1 2
| build.gradle(app) compile 'in.srain.cube:ultra-ptr:1.0.11'
|
布局文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| <in.srain.cube.views.ptr.PtrClassicFrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/refresh_layout" android:layout_width="match_parent" android:layout_marginTop="?attr/actionBarSize" android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView android:id="@+id/news_rv" android:layout_width="match_parent" android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
</in.srain.cube.views.ptr.PtrClassicFrameLayout>
|
刷新操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| mRefreshLayout=(PtrFrameLayout)findViewById(R.id.refresh_layout); mRefreshLayout.setLoadingMinTime(1000); mRefreshLayout.setPullToRefresh(true); ... mRefreshLayout.setPtrHandler(new PtrHandler() { @Override public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
return PtrDefaultHandler.checkContentCanBePulledDown(frame,content, header); }
@Override public void onRefreshBegin(PtrFrameLayout frame) { pageNum+=1; updateDatas(service,apiKey,pageNum); } });
public void updateDatas(Api.NewsService service,String apiKey,int pageNum){ service.getList(apiKey,pageNum) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Subscriber<News>() { @Override public void onCompleted() { }
@Override public void onError(Throwable e) { }
@Override public void onNext(News news) { datas=news.getShowapi_res_body().getPagebean().getContentlist(); notifyDatasetChanged(datas); mRefreshLayout.refreshComplete(); } }); }
|