Android-PullToRefresh:下拉刷新和加载更多

基本概念

Jar vs Library Project

Jar:打包的的是.class文件,只有java code,目前还不支持包含resources.「如果只有code,那么适合jar方式」

Library Project:可以java code,resource「layouts,themes etc」「如果你需要重用res文件,那么适合Android library project」

开源项目:Android-PullToRefresh 「Library Project的方式」

1. 如果你使用ant打包,那么在 project.properties添加

1 android.library.reference.1=../Android-PullToRefresh 「相对路径」
2 //或者执行命令,会改变project.properties,local.properties
3 android update project -p . --library ../Android-PullToRefresh</pre>
4 Updated project.properties
5 Updated local.properties
6 需要检查下上面updated的文件「有可能重复」

2. 使用Android Studio

1 File -> Import Module
2 添加依赖「add Android-PullToRefresh as a dependency to project」

ListView支持下拉刷新:官方Quick-Start-Guide

1. layout添加内容:

1 <!--     The PullToRefreshListView replaces a standard ListView widget. -->
2     <com.handmark.pulltorefresh.library.PullToRefreshListView
3         android:id="@+id/pull_refresh_list"
4         android:layout_width="fill_parent"
5         android:layout_height="fill_parent"
6         android:cacheColorHint="#00000000"
7         android:divider="#19000000"
8         android:dividerHeight="4dp"
9         android:fadingEdge="none"
10         android:fastScrollEnabled="false"
11         android:footerDividersEnabled="false"
12         android:headerDividersEnabled="false"
13         android:smoothScrollbar="true" />

2. 代码修改「如果你已经存在listview的话,包括Adapter」

1 public class demoListActivity extends Activity implementsPullToRefreshBase.OnRefreshListener<ListView> {
2  
3     //定义成员变量:
4     private PullToRefreshListView mPullRefreshListView;
5  
6     onCreate(){
7         //赋值listview
8         mPullRefreshListView = (PullToRefreshListView) findViewById(R.id.pull_refresh_list);
9         //设置下拉或上拉监听
10         mPullRefreshListView.setOnRefreshListener(this);
11         ...
12     }
13  
14      //监听接口实现
15     @Override
16     public void onRefresh(PullToRefreshBase<ListView> refreshView) {
17         onRefresh();//刷新操作
18     }
19  
20     //onRefresh()完成后调用:mPullRefreshListView.onRefreshComplete();
21 }

这么几步,你的ListView就支持下拉刷新了。

上拉加载更多:PullToRefreshBase.Mode 设置

1 //默认是只打开了下拉刷新,可以设置支持上拉加载更多。
2     mPullRefreshListView.setMode(PullToRefreshBase.Mode.BOTH);
3     public static Mode PULL_DOWN_TO_REFRESH = Mode.PULL_FROM_START;//从顶部下拉
4     public static Mode PULL_UP_TO_REFRESH = Mode.PULL_FROM_END;//从底部上拉
5  
6     @Override
7     public void onRefresh(PullToRefreshBase<ListView> refreshView) {
8         // Do work to refresh the list here.
9         //判断上拉 还是 下拉
10         if (PullToRefreshBase.Mode.PULL_FROM_START == mPullRefreshListView.getCurrentMode()) {
11             onRefresh();
12         else if (PullToRefreshBase.Mode.PULL_FROM_END == mPullRefreshListView.getCurrentMode()){
13             onLoadMore();
14         }
15     }

数据为空时候的显示

1 //当数据为空的时候,也可以设置EmptyDataView
2 mPullRefreshListView.setEmptyView(emptyView);

pull最大的距离

1 //PullToRefreshBase 中有一个变量,默认是屏幕的1/2,感觉太长了,改成了1/3
2 static final float FRICTION = 3.0f;//上拉和下拉的最大距离 1/3
3      private int getMaximumPullScroll() {
4           switch (getPullToRefreshScrollDirection()) {
5                case HORIZONTAL:
6                     return Math.round(getWidth() / FRICTION);
7                case VERTICAL:
8                default:
9                     return Math.round(getHeight() / FRICTION);
10           }
11      }

新发现有上拉、下拉的接口:PullToRefreshBase.OnRefreshListener2

1 public class demoListActivity extends Activity implementsPullToRefreshBase.OnRefreshListener2<ListView> {
2     mPullRefreshListView.setOnRefreshListener(this);
3 }
4  
5     @Override
6     public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
7          
8     }
9  
10     @Override
11     public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
12         //如果没有更多了,需要这样完成上拉加载更多
13         if(no more){
14             refreshView.post(new Runnable() {
15                 @Override
16                 public void run() {
17                     mPullRefreshListView.onRefreshComplete();
18                 }
19             });
20         }
21         return;
22         ...
23     }

你可能感兴趣的:(Android-PullToRefresh:下拉刷新和加载更多)