滾動到底部加載更多及下拉刷新listview的使用

最新內容建議直接訪問原文:滾動到底部加載更多及下拉刷新listview的使用java

 

本文主要介紹可同時實現下拉刷新及滑動到底部加載更多的ListView的使用。android

該ListView優勢包括:a. 可自定義下拉響應事件(以下拉刷新)  b.可自定義滾動到底部響應的事件(如滑動到底部加載更多)  c.可自定義豐富的樣式  d.高效(若下拉樣式關閉不會加載其佈局,同listView效率一致) e. 豐富的設置。git

 

本文可運行APK地址可見TrineaAndroidDemo.apk,可運行代碼地址可見DropDownListViewDemo,效果圖以下:github

 

一、引入公共庫app

引入TrineaAndroidCommon@Github(歡迎star和fork^_^)做爲你項目的library(如何拉取代碼及添加公共庫),或是本身抽取其中的DropDownListView@Github部分使用。ide

 

二、在layout中定義
將佈局中的ListView標籤換成cn.trinea.android.common.view.DropDownListView標籤
並加上自定義屬性的命名空間xmlns:listViewAttr="http://schemas.android.com/apk/res/cn.trinea.android.demo",其中cn.trinea.android.demo須要用本身的包名替換。如何自定義屬性及其命名空間可見本文最後。xml代碼以下:佈局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:listViewAttr="http://schemas.android.com/apk/res/cn.trinea.android.demo"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <cn.trinea.android.common.view.DropDownListView
        android:id="@+id/list_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:drawSelectorOnTop="false"
        android:paddingBottom="@dimen/dp_40"
        listViewAttr:isDropDownStyle="true"
        listViewAttr:isOnBottomStyle="true"
        listViewAttr:isAutoLoadOnBottom="true" />

</RelativeLayout>

 

isDropDownStyle表示是否容許下拉樣式,java代碼中可自定義下拉listener,表示須要完成的任務性能

isOnBottomStyle表示是否容許底部樣式,java代碼中可自定義滾動到底部的listener,表示須要完成的任務
isAutoLoadOnBottom表示是否容許滾動到底部時自動執行對應listener,僅在isOnBottomStyle爲true時有效this

PS:若是isDropDownStyle或isOnBottomStyle爲false,並不會加載對應的佈局,因此性能同ListView同樣google

 

三、在Java類中調用
經過setOnDropDownListener設置下拉的事件,不過須要在事件結束時手動調用onDropDownComplete恢復狀態
經過setOnBottomListener設置滾動到底部的事件,不過須要在事件結束時手動調用onBottomComplete恢復狀態,示例代碼以下:

/**
 * DropDownListViewDemo
 * 
 * @author Trinea 2013-6-1
 */
public class DropDownListViewDemo extends BaseActivity {

    private LinkedList<String>   listItems = null;
    private DropDownListView     listView  = null;
    private ArrayAdapter<String> adapter;

    private String[]             mStrings  = { "Aaaaaa", "Bbbbbb", "Cccccc", "Dddddd", "Eeeeee",
            "Ffffff", "Gggggg", "Hhhhhh", "Iiiiii", "Jjjjjj", "Kkkkkk", "Llllll", "Mmmmmm",
            "Nnnnnn",                     };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState, R.layout.drop_down_listview_demo);

        listView = (DropDownListView)findViewById(R.id.list_view);
        // set drop down listener
        listView.setOnDropDownListener(new OnDropDownListener() {

            @Override
            public void onDropDown() {
                new GetDataTask(true).execute();
            }
        });

        // set on bottom listener
        listView.setOnBottomListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                new GetDataTask(false).execute();
            }
        });

        listItems = new LinkedList<String>();
        listItems.addAll(Arrays.asList(mStrings));
        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listItems);
        listView.setAdapter(adapter);
    }

    private class GetDataTask extends AsyncTask<Void, Void, String[]> {

        private boolean isDropDown;

        public GetDataTask(boolean isDropDown){
            this.isDropDown = isDropDown;
        }

        @Override
        protected String[] doInBackground(Void... params) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                ;
            }
            return mStrings;
        }

        @Override
        protected void onPostExecute(String[] result) {

            if (isDropDown) {
                listItems.addFirst("Added after drop down");
                adapter.notifyDataSetChanged();

                // should call onDropDownComplete function of DropDownListView at end of drop down complete.
                SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd HH:mm:ss");
                listView.onDropDownComplete(getString(R.string.update_at)
                                            + dateFormat.format(new Date()));
            } else {
                listItems.add("Added after on bottom");
                adapter.notifyDataSetChanged();

                // should call onBottomComplete function of DropDownListView at end of on bottom complete.
                listView.onBottomComplete();
            }

            super.onPostExecute(result);
        }
    }
}
Java部分代碼

 

四、高級接口設置

五、樣式設置(自定義header和footer信息)
見原文:滾動到底部加載更多及下拉刷新listview的使用

相關文章
相關標籤/搜索