使用pullToRefresh进行下拉刷新和上拉加载
2017-11-29 20:41
351 查看
/** * 使用pullToRefresh进行下拉刷新和上拉加载 * 1.去github上下载压缩包,解压出来,把library复制出来改名,路径最好不要有中文 * 2.使用import moundle导进这个库文件,添加库文件的依赖 * 3.此时左下角可能会报错,只需要点击install...去下载16版本的sdk,还需要下载19版本的buildTools * * 查看代码可以看到 * PullToRefreshListView可以刷新的listView控件....间接继承了LinearLayout,是一个自定义的listView * PullToRefreshGridView可以刷新的GridView控件 * PullToRefreshExpandableListView可以刷新的二级列表控件 * PullToRefreshScrollView可以刷新的ScrollView控件 * PullToRefreshWebView可以刷新的WebView控件 */
pullToRefreshListView的基本使用
pullToRefreshListView和ListView的使用基本差的不多,只不过ListView的xml要换成com.handmark.pulltorefresh.library.PullToRefreshListView例子如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <com.handmark.pulltorefresh.library.PullToRefreshListView xmlns:ptr="http://schemas.android.com/apk/res-auto" android:layout_height="match_parent" android:layout_width="match_parent" android:id="@+id/refresh_list_view" ptr:ptrDrawable="@drawable/default_ptr_flip" ptr:ptrAnimationStyle="flip" ptr:ptrHeaderBackground="#383838" ptr:ptrHeaderTextColor="#FFFFFF" > </com.handmark.pulltorefresh.library.PullToRefreshListView> </LinearLayout>
//PullToRefreshGridView的布局
<com.handmark.pulltorefresh.library.PullToRefreshGridView xmlns:ptr="http://schemas.android.com/apk/res-auto" android:layout_height="match_parent" android:layout_width="match_parent" android:id="@+id/refreshGrid" ptr:ptrDrawable="@drawable/default_ptr_flip" ptr:ptrAnimationStyle="flip" ptr:ptrHeaderBackground="#383838" ptr:ptrHeaderTextColor="#FFFFFF" android:numColumns="2">
//
PullToRefreshScrollView加viewpager加圆点加listview布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <com.handmark.pulltorefresh.library.PullToRefreshScrollView xmlns:ptr="http://schemas.android.com/apk/res-auto" android:layout_height="match_parent" android:layout_width="match_parent" android:id="@+id/refreshScroll" ptr:ptrDrawable="@drawable/default_ptr_flip" ptr:ptrAnimationStyle="flip" ptr:ptrHeaderBackground="#383838" ptr:ptrHeaderTextColor="#FFFFFF" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <RelativeLayout android:layout_width="match_parent" android:layout_height="300px" > <android.support.v4.view.ViewPager android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/vp1" ></android.support.v4.view.ViewPager> <LinearLayout android:layout_alignParentBottom="true" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:orientation="horizontal" android:id="@+id/linearLayout"></LinearLayout> </RelativeLayout> <one.bw.com.wangchuang1023.util.MyListView android:id="@+id/lv" android:layout_width="match_parent" android:layout_height="match_parent"/> </LinearLayout> </com.handmark.pulltorefresh.library.PullToRefreshScrollView> </LinearLayout>
/*** 获取网络数据 也是首次加载数据*/private void getDataFromNet() {String path="http://gank.io/api/data/Android/10/1";NetgetcountUtil.getcount(path, getActivity(), new JieKou() {@Overridepublic void chuan(String json) {Gson gson = new Gson();Mybean mybean = gson.fromJson(json, Mybean.class);List<Mybean.ResultsBean> results = mybean.getResults();list.clear();list.addAll(results);setAtapter();refreshList.onRefreshComplete();Date date = new Date(System.currentTimeMillis());SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");String format = sim.format(date);refreshList.setLastUpdatedLabel(format);}});}步骤一 绑定控件,设置属性绑定控件代码如下:
private PullToRefreshListView listview;listview = (PullToRefreshListView) findViewById(R.id.refresh_list_view);
//2.设置刷新模式/*设置pullToRefreshListView的刷新模式,BOTH代表支持上拉和下拉,PULL_FROM_END代表上拉,PULL_FROM_START代表下拉 */
//对pullToListView绑定adapterlistview.setAdapter(adapter);/*设置pullToRefreshListView的刷新模式,BOTH代表支持上拉和下拉,PULL_FROM_END代表上拉,PULL_FROM_START代表下拉 */listview.setMode(PullToRefreshBase.Mode.BOTH);initRefreshListView(); //设置刷新显示的状态的方法
//3.通过getLoadingLayoutProxy 方法来指定上拉和下拉时显示的状态的区别(也就是设置向下拉的时候头部里面显示的文字)//此时这里设置的是下拉刷新的时候显示的文字,所以第一个设置true表示现在是刷新,第二个设置为false
public void initRefreshListView(){
ILoadingLayout startLabels = refreshList.getLoadingLayoutProxy(true, false);startLabels.setPullLabel("下拉刷新");startLabels.setRefreshingLabel("正在拉");startLabels.setReleaseLabel("放开刷新");ILoadingLayout endLabels = refreshList.getLoadingLayoutProxy(false, true);endLabels.setPullLabel("上拉刷新");endLabels.setRefreshingLabel("正在载入...");endLabels.setReleaseLabel("放开刷新...");
//4.设置监听事件refreshList.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {//下拉刷新的时候调用的方法....请求第一页的数据,清空之前的数据,然后再添加设置适配器@Overridepublic void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {getDataFromNet();}//上拉刷新的时候调用的方法..page++,然后在请求数据@Overridepublic void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {i++;String path="http://gank.io/api/data/Android/10/"+i;NetgetcountUtil.getcount(path, getActivity(), new JieKou() {@Overridepublic void chuan(String json) {Gson gson = new Gson();Mybean mybean = gson.fromJson(json, Mybean.class);List<Mybean.ResultsBean> results = mybean.getResults();//list.clear();list.addAll(results);setAtapter();refreshList.onRefreshComplete();//完成刷新,关闭刷新Date date = new Date(System.currentTimeMillis());SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");String format = sim.format(date);refreshList.setLastUpdatedLabel(format);//添加时间}});}});
相关文章推荐
- 使用pullToRefresh进行下拉刷新和上拉加载
- 使用pullToRefresh进行下拉刷新和上拉加载
- listView利用BaseAdapter及异步任务Gson解析,使用Picasso网络获取资源进行图文混排,PullToRefresh上拉刷新下拉加载
- 关于PullToRefresh对于数据的解析和上拉刷新下拉加载
- android使用PullToRefresh实现上拉加载和下拉刷新效果
- PullToRefresh上拉刷新下拉加载更多的使用
- 【Android-PullToRefresh】 上拉刷新、下拉加载 使用详解
- 使用Android开源工具PullToRefresh实现上啦刷新下拉加载
- android使用PullToRefresh实现上拉加载和下拉刷新效果
- android使用PullToRefresh实现上拉加载和下拉刷新效果
- Android使用PullToRefresh实现上拉加载和下拉刷新效果的代码
- PullToRefresh 上拉刷新,下拉加载数据 框架的简单使用
- 使用PullToRefresh实现下拉刷新和上拉加载
- 4.5.1 Good 使用PullToRefresh实现下拉刷新和上拉加载
- 使用PullToRefresh实现下拉刷新和上拉加载
- 针对自定义组件上拉刷新下拉加载更多PullToRefreshView的分析(二)
- 使用PullToRefresh实现下拉刷新和上拉加载
- 使用PullToRefresh实现下拉刷新和上拉加载
- 针对自定义组件上拉刷新下拉加载更多PullToRefreshView的分析(一)
- 使用自定义的item、Adapter和AsyncTask、第三方开源框架PullToRefresh联合使用实现自定义的下拉列表(从网络加载图片显示在item中的ImageView)