該庫支持使用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