该库支持使用ScrollView, ListView和RecyclerView三大控件场景下的下拉刷新和上拉加载功能,若是在使用过程当中发现缺陷,欢迎指正。java
该控件在布局中做为Parent Layout包裹如ScrollView、ListView或RecyclerView使用,只支持下拉刷新功能(相似于官方的SwipeRefreshLayout), 使用流程以下:android
<?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>
//须要手动设置背景 AnimationDrawable mAnimationDrawable = (AnimationDrawable) ResourcesCompat.getDrawable(getResources(), R.drawable.windshield_wiper, null); mPullToRefreshView.setRefreshFrame(new BaseFrameView(mAnimationDrawable, mPullToRefreshView));
//须要手动设置背景 View mTopWidget = (ViewGroup) LayoutInflater.from(this).inflate(R.layout.widget_home, null); mPullToRefreshView.addCustomeView(mTopWidget);
mPullToRefreshView.setOnRefreshListener(new PullToRefreshView.OnRefreshListener() { @Override public void onRefresh() { //处理下拉刷新动做 } });
主动开始下拉刷新动做:ide
mPullToRefresh.setRefreshing(true);
主动结束下拉刷新动做:布局
mPullToRefresh.setRefreshing(false);
该控件是基于ListView的包装类,用于支持其下拉刷新和上拉加载功能,同时支持预加载功能(若是未显示到最后一项将继续请求下一页的数据),使用方式等同于ListView。this
<?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
PullToRefreshListView mProListView = (PulltoRefreshListView) this.findViewById(R.id.favorite_pro_listview); mProListView.setAdapter(listAdapter);
//设置下拉刷新的监听回调 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() {//当取消上拉加载时触发 } });
该控件是基于RecyclerView的包装类,用于支持其下拉刷新和上拉加载功能,使用方式等同于RecyclerView。code
<?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>
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