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

简介:

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

一. PullToRefreshView

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

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>

自定义属性:app

<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() {
        //处理下拉刷新动做
    }
});

其余方法:

主动开始下拉刷新动做:ide

mPullToRefresh.setRefreshing(true);

主动结束下拉刷新动做:布局

mPullToRefresh.setRefreshing(false);

 

二. PullToListView

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

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>

自定义属性:spa

  • 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。code

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相同orm

相关文章
相关标签/搜索