常用的刷新技术(三)——PullToRefreshlibrary

PullToRefreshlibrary 下拉刷新库

一、简介

PullToRefreshlibrary 为我们封装好了各种自定义View,可以直接在布局文件中引用,并且都带有下拉刷新和上拉加载的功能,给我们带来了很多方便,不像SwipeRefresh以及Ultra-PullToRefresh那样需要自己编写上拉加载的方法。

二、效果图

1.下拉刷新

这里写图片描述

这里写图片描述

2.上拉加载

这里写图片描述

这里写图片描述

三、使用步骤

1.导入第三方模块:PullToRefreshlibrary

这里写图片描述

通过可视化界面关联到我们的项目中

这里写图片描述

点击“同步”

2.布局文件

例1:

<com.handmark.pulltorefresh.library.PullToRefreshScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ptr= "http://schemas.android.com/apk/res-auto"
    android:id="@+id/pullToRefreshScrollView_hot"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    ptr:ptrMode= "both"
    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:padding="10dp"
        >

    //根据需要在这里编写控件

    </LinearLayout>
</com.handmark.pulltorefresh.library.PullToRefreshScrollView>

注:必须给PullToRefreshScrollView设置属性ptr:ptrMode= “both”才能支持上拉加载。

例2:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:fresco="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp"
    android:orientation="vertical">

    <com.handmark.pulltorefresh.library.PullToRefreshGridView
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:ptr="http://schemas.android.com/apk/res-auto"
        android:id="@+id/pullToRefreshGridView_editorRecommend"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:numColumns="3"
        android:horizontalSpacing="5dp"
        android:verticalSpacing="10dp"
        ptr:ptrMode="both">

    </com.handmark.pulltorefresh.library.PullToRefreshGridView>
</LinearLayout>

注:将PullToRefreshGridView包裹在LinearLayout中。

例3:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:fresco="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp"
    android:orientation="vertical">

    <com.handmark.pulltorefresh.library.PullToRefreshListView
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:ptr="http://schemas.android.com/apk/res-auto"
        android:id="@+id/pullToRefreshListView_update"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:horizontalSpacing="5dp"
        android:verticalSpacing="10dp"
        ptr:ptrMode="both">

    </com.handmark.pulltorefresh.library.PullToRefreshListView>
</LinearLayout>

注:将PullToRefreshListView包裹在LinearLayout中。

3.Java代码:设置下拉刷新和上拉加载的监听器

PullToRefreshListView、PullToRefreshGridView、PullToRefreshScrollView的设置监听器的方法都一样。我们以PullToRefreshScrollView为例:

pullToRefreshScrollView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ScrollView>() {
                    @Override
                    public void onPullDownToRefresh(PullToRefreshBase<ScrollView> refreshView) {
                        //下拉刷新
                    }

                    @Override
                    public void onPullUpToRefresh(PullToRefreshBase<ScrollView> refreshView) {
                        //上拉加载更多
                    }
                });

注:选择设置监听器时,系统会提示有两个监听器,此时我们选择带“2”的监听器。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

陈有余v

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值