PullRefresh - 一個支持下拉刷新和上拉刷新的控件

jopen 8年前發布 | 10K 次閱讀 Android開發 移動開發 PullRefresh

一個支持下拉刷新和上拉刷新的控件,并且支持自定義下拉頭和上拉頭,可選擇只使用下拉頭或者只使用上拉頭

Android Arsenal

Demo

點擊觀看視頻

下拉刷新

上拉刷新

Usage

導入三個文件

1.PullRefresh.java  
2.HeaderHolder.java  
3.FooterHolder.java  

自定義header.xml和footer.xml

使用的地方

<com.ylqhust.pullrefresh.PullRefresh
    android:id="@+id/pullrefresh"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <include layout="@layout/header"/>
    <ListView
        android:id="@+id/listview"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </ListView>
    <include layout="@layout/footer"/>
</com.ylqhust.pullrefresh.PullRefresh>

代碼部分

pullRefresh = (PullRefresh) findViewById(R.id.pullrefresh);
    listView = (ListView) findViewById(R.id.listview);
    View header = findViewById(R.id.header);
    View footer = findViewById(R.id.footer);

    PullRefresh.CallBack callBack = new PullRefresh.CallBack() {
        @Override
        public boolean CanotPullDown() {
            if (listView.getFirstVisiblePosition() == 0) {
                View first = listView.getChildAt(0);
                if (first.getTop() == 0)
                    return true;
            }
            return false;
        }

        @Override
        public boolean CanotPullUp() {
            if (listView.getLastVisiblePosition() == listView.getCount() - 1) {
                View last = listView.getChildAt(listView.getChildCount() - 1);
                if (Math.abs(listView.getBottom() - last.getBottom()) < 2)
                    return true;
            }
            return false;
        }
    };
    PullRefresh.Task task = new PullRefresh.Task() {
        @Override
        public void HeaderTask() {
            new Thread() {
                @Override
                public void run() {
                    try {
                        sleep(3000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            HeaderTaskFinish();
                        }
                    });
                }
            }.start();
        }

        @Override
        public void FooterTask() {
            new Thread() {
                @Override
                public void run() {
                    try {
                        sleep(3000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            FooterTaskFinish();
                        }
                    });
                }
            }.start();
        }
    };
    //使用頭部和尾部
    pullRefresh.UseAll(callBack,header, listView,footer, new SimpleHeaderHolder(this), new SimpleFooterHolder(this),task );
    //只使用頭部
    //pullRefresh.UseHeader(callBack,header,listView,new SimpleHeaderHolder(this),task);
    //只使用尾部
    //pullRefresh.UseFooter(callBack,footer,listView,new SimpleFooterHolder(this),task);


    List<String> stringList = new ArrayList<String>();
    for(int i=0;i<20;i++){
        stringList.add("String:"+i);
    }

    adapter = new Adapter(this,stringList);
    listView.setAdapter(adapter);
}

private void HeaderTaskFinish() {
    if (tag%2==0){
        pullRefresh.headerEndSuccess();
        adapter.addDataToHeader();
        adapter.notifyDataSetChanged();
    }
    else{
        pullRefresh.headerEndFailed();
    }
    tag++;
}

private void FooterTaskFinish() {
    if (tag%2==0){
        pullRefresh.footerEndSuccess();
        adapter.addDataToFooter();
        adapter.notifyDataSetChanged();
    }
    else{
        pullRefresh.footerEndFailed();
    }
    tag++;
}

項目地址: https://github.com/ylqhust/PullRefresh

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!