您的位置:首页 > 其它

使用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);//添加时间}});}});

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: