PullToRefresh库的用法(供内部分享)

简介:

该库支持使用ScrollView, ListView和RecyclerView三大控件场景下的下拉刷新和上拉加载功能,如果在使用过程中发现缺陷,欢迎指正。

一. PullToRefreshView

该控件在布局中作为Parent Layout包裹如ScrollView、ListView或RecyclerView使用,只支持下拉刷新功能(类似于官方的SwipeRefreshLayout), 使用流程如下:

Step1:在布局文件中作为父布局引入

<?xml version="1.0" encoding="utf-8"?>
<com.yalantis.phoenix.PullToRefreshView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/pull_to_refresh"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:anim_type_view="sun">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true"
        android:overScrollMode="never"
        android:scrollbars="none">

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

            <TextView
                android:id="@+id/tv_title"
                android:layout_width="match_parent"
                android:layout_height="100dp"
                android:background="@android:color/holo_red_light"
                android:text="Hello World!" />

        </LinearLayout>
    </ScrollView>
</com.yalantis.phoenix.PullToRefreshView>

自定义属性:

    <declare-styleable name="RefreshView">
        <attr name="anim_type_view" format="enum">
            <enum name="sun" value="0" />
            <enum name="frame" value="1" />
            <enum name="custom" value="2" />
        </attr>
    </declare-styleable>
  • sun:使用默认的下拉头部背景;
  • frame:设置AnimationDrawable作为下拉头部背景;
  •     //需要手动设置背景
        AnimationDrawable mAnimationDrawable = (AnimationDrawable) 
        ResourcesCompat.getDrawable(getResources(), R.drawable.windshield_wiper, null);
    
        mPullToRefreshView.setRefreshFrame(new BaseFrameView(mAnimationDrawable, mPullToRefreshView));
  • custom:设置自定义布局作为下拉头部背景;
  •     //需要手动设置背景
        View mTopWidget = (ViewGroup) LayoutInflater.from(this).inflate(R.layout.widget_home, null);
        mPullToRefreshView.addCustomeView(mTopWidget);

Step2:设置监听下拉刷新动作的回调


 mPullToRefreshView.setOnRefreshListener(new PullToRefreshView.OnRefreshListener() {
    @Override
    public void onRefresh() {
        //处理下拉刷新动作
    }
});

其他方法:

主动开始下拉刷新动作:

    mPullToRefresh.setRefreshing(true);

主动结束下拉刷新动作:

    mPullToRefresh.setRefreshing(false);

 

二. PullToListView

该控件是基于ListView的包装类,用于支持其下拉刷新和上拉加载功能,同时支持预加载功能(如果未显示到最后一项将继续请求下一页的数据),使用方式等同于ListView。

Step1:在布局文件中引入

<?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.yalantis.phoenix.PulltoRefreshListView 
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/cprl_coupon"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/view_coupon"
        android:layout_below="@id/rl_title_coupon_activity"
        app:listViewLayout="@layout/layout_coupon_list"
        app:progressLayout="@layout/layout_progress"
        app:emptyLayout="@layout/layout_empty"
        app:loadMoreEnd="@layout/layout_more_end"
        app:loadMoreProgress="@layout/layout_more_progress"
        app:loadMoreClick="@layout/layout_more_click"/>
</LinearLayout>

自定义属性:

  • listViewLayout属性:引入一个ListView布局
  • progressLayput属性:引入刷新或加载时的进度条布局
  • emptyLayout属性:引入当数据为空时的文案布局(默认为"数据为空")
  • loadMoreEnd属性:引入当已经拉到最后一项时底部的提示文案布局 (默认为"------回头是岸------")
  • loadMoreProgress属性:引入当正在加载下一页时底部的提示文案布局(默认为“正在努力加载数据...")
  • loadMoreClick属性:引入当还有下一页时,未触发上拉加载时的提示文案布局 (默认为“点击加载更多”)

Step2:设置适配器

    PullToRefreshListView mProListView = (PulltoRefreshListView) this.findViewById(R.id.favorite_pro_listview);
    mProListView.setAdapter(listAdapter);

Step3:设置下拉刷新和上拉加载的监听回调


    //设置下拉刷新的监听回调
    mProListView.setOnRefreshListener(new PullToRefreshView.OnRefreshListener() {
         @Override
         public void onRefresh() {
             mPresenter.refreshProList();
         }
    });

    //设置上拉加载的监听回调
    mProListView.setOnLoadMoreListener(new BaseRefreshView.OnLoadMoreListener() {
         @Override
         public void onLoadMore() {//当下一页还有数据时上拉触发
             mPresenter.loadMorePro();
         }

         @Override
         public boolean needLoadMore() {//判断是否已滑到底部
             return !mPresenter.ifProArrivedLast();
         }

         @Override
         public void cancelLoadMore() {//当取消上拉加载时触发

         }
     });

其他方法:

  • void setRefreshing(boolean result):主动开始或停止执行下拉刷新动作;
  • void setFinishLoadMore():主动结束上拉加载动作;
  • void setRefreshEnable(boolean result):设置是否支持下拉刷新操作;
  • View setEmptyView(int resId):设置数据为空时的背景;
  • void setFootetView(View footerView):设置底部显示布局

 

三. PullToRecyclerView

该控件是基于RecyclerView的包装类,用于支持其下拉刷新和上拉加载功能,使用方式等同于RecyclerView。

Step1:在布局文件中引入

<?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"
    android:background="@color/c_F0">

    <com.yalantis.phoenix.PulltoRefreshRecyclerView 
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/recycler_store_coupon_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/dp_8"
        android:paddingLeft="@dimen/dp_12"
        android:paddingRight="@dimen/dp_12"
        android:paddingTop="@dimen/dp_8"
        app:listViewLayout="@layout/layout_coupon_list"
        app:progressLayout="@layout/layout_progress"
        app:emptyLayout="@layout/layout_empty"
        app:loadMoreEnd="@layout/layout_more_end"
        app:loadMoreProgress="@layout/layout_more_progress"
        app:loadMoreClick="@layout/layout_more_click"/>
</LinearLayout>

Step2:设置适配器

    PulltoRefreshRecyclerView mStoreCouponList = (PulltoRefreshRecyclerView) findViewById(R.id.recycler_store_coupon_list);
    mStoreCouponList.getRecyclerView().setHasFixedSize(false);
    mStoreCouponList.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
    mStoreCouponList.addItemDecoration(new DividerLayoutDecoration(this, LinearLayoutManager.VERTICAL));
    mStoreCouponList.setAdapter(new RecyclerView.Adapter())

设置监听流程和具体方法与PullToRefreshListView相同

转载于:https://my.oschina.net/u/3389024/blog/886227

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值