PullToRefresh--可自定義的下拉刷新上拉加載庫

PullToRefresh

開發者能夠使用 PullToRefresh 對各類控件實現下拉刷新或者上拉加載以及能夠自定義刷新和加載部分的視圖。java

目前支持:ScrollView ListViewWebViewRecyclerViewandroid

github地址:https://github.com/823546371/PullToRefreshgit

碼雲地址:http://git.oschina.net/jinwenfeng/PullToRefreshgithub

歡迎star or forkexpress

版本記錄

v1.0.3

修復了滑動最小距離判斷apache

v1.1.0

新增了空數據,加載中以及網絡錯誤的3種狀態頁面網絡

效果圖

基本用法

一、添加 Gradle 依賴

目前只支持 Android Studioapp

compile 'com.jwenfeng.pulltorefresh:library:1.1.0'

二、佈局文件

注意:內容控件 有且只能有一個,目前支持:ScrollView ListViewWebViewRecyclerViewless

<?xml version="1.0" encoding="utf-8"?>
<com.jwenfeng.library.pulltorefresh.PullToRefreshLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    
    <!-- 內容控件 有且只能有一個 -->
     <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
     </ScrollView>
    
</com.jwenfeng.library.pulltorefresh.PullToRefreshLayout>

三、在Activity或者Fragment中使用

pullToRefreshLayout.setRefreshListener(new BaseRefreshListener() {
            @Override
            public void refresh() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        // 結束刷新
                        pullToRefreshLayout.finishRefresh();
                    }
                }, 2000);
            }

            @Override
            public void loadMore() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        // 結束加載更多
                        pullToRefreshLayout.finishLoadMore();
                    }
                }, 2000);
            }
        });

四、自定義視圖

自定義下拉刷新視圖須要實現 HeadView 接口ide

public interface HeadView {

    /**
     * 開始下拉
     */
    void begin();

    /**
     * 回調的精度,單位爲px
     *
     * @param progress 當前高度
     * @param all      總高度
     */
    void progress(float progress, float all);

    void finishing(float progress, float all);
    /**
     * 下拉完畢
     */
    void loading();

    /**
     * 看不見的狀態
     */
    void normal();

    /**
     * 返回當前視圖
     * */
    View getView();

}

具體請參考Demo中 HeadRefreshView

上拉加載更多用法和下拉刷新相似,具體參考 LoadMoreView.java

五、其餘

能夠設置下拉刷新和上拉加載控件的高度和拉取的最大高度,默認爲60dp,最大拉取爲120dp,可自行設置。

六、v1.1.0 新增自定義3中狀態頁面

截圖

用法

XML
<com.jwenfeng.library.pulltorefresh.PullToRefreshLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/activity_recycler_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:view_error="@layout/layout_error"
    app:view_empty="@layout/layout_empty"
    app:view_loading="@layout/layout_loading">

</com.jwenfeng.library.pulltorefresh.PullToRefreshLayout>

app:view_error 網絡錯誤頁面

app:view_empty 空數據頁面

app:view_loading 加載中頁面

該三個屬性能夠不填寫,默認樣式爲上圖所示,能夠自定義

java用法
pullToRefreshLayout.showView(ViewStatus.LOADING_STATUS);

設置須要顯示的視圖

// 獲取頁面
View error = pullToRefreshLayout.getView(ViewStatus.ERROR_STATUS);

獲取所須要的視圖view

注意 必定要在調用了設置相應的視圖以後才能夠調用獲取頁面,不然view爲空

License

Copyright 2016-2017 jinwenfeng

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
相關文章
相關標籤/搜索