ListView 分段顯示

來源:互聯網
上載者:User

Android market裡軟體列表,每頁顯示10條記錄,沒有顯示上一頁,下一頁的按鈕,依靠手滑動動態載入資料,當向下滾動時,最下邊顯示 Loading… 。資料載入結束,Loading底欄消失。

關於ListView的分段顯示,有現成的庫可用,比如 cwac-endless, 這個庫不好之處,就是底部Loading的View無法定製。還有一個在google code上的androidpageablelistview 這個可以實現基本的分頁,有手動操作顯示上一頁,下一頁的按鈕。

實現思路如下:
自訂 footer view, list_footer.xml:

<?xml version="1.0" encoding="utf-8"?><LinearLayout    xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="wrap_content"    >    <LinearLayout        android:gravity="center_vertical|center_horizontal"        android:orientation="horizontal"        android:id="@+id/loading_more"        android:visibility="gone"        android:layout_width="fill_parent"        android:layout_height="?android:attr/listPreferredItemHeight"        >        <ProgressBar            android:id="@+android:id/progress_large"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:indeterminate="true"            style="?android:attr/progressBarStyleSmall"            >        </ProgressBar>        <TextView            android:id="@+id/loading_msg"            android:paddingLeft="6.0dip"            android:paddingTop="2.0dip"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_marginBottom="1.0dip"            android:text="@string/loading"            >        </TextView>    </LinearLayout></LinearLayout>

用到 ListView addFooterView/removeView 這兩個函數。

虛擬碼 (Pseudocode):

private ListView mListView;private View mFooterView;private OnScrollListener mOnListViewScrollListener;mListView.addFooterView(mFooterView);mListView.removeView(mFooterView);mListView.setOnScrollListener(mOnListViewScrollListener);mOnListViewScrollListener = new OnScrollListener() {    public void onScroll(AbsListView view, int firstCell, int cellCount,            int itemCount) {        if (firstCell != mFirstCell) {            // balabala        }    }    public void onScrollStateChanged(AbsListView view, int scrollState) {        // Do nothing    }}; 

在onScroll裡要處理檢查是否還有新的記錄,如果有,調用addFooterView,添加記錄到adapter, adapter調用 notifyDataSetChanged 更新資料;如果沒有記錄了, 把自訂的mFooterView去掉。這裡沒有重寫onScrollStateChanged函數,那麼在onScroll就需要一個外部變數mFirstCell記錄滑動位置。

再看看QQ空間體驗版 for Android 是如何?的,不用多說,show me the code:

public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {    shouldRefresh = false;    if (firstVisibleItem + visibleItemCount == totalItemCount) {        if (bHaveMore()) {            if (list.getFooterViewsCount() == 0) {                addRefreshfoot();            } else {                shouldRefresh = true;            }        }    }}public void onScrollStateChanged(AbsListView view, int scrollState) {    if (shouldRefresh && scrollState == OnScrollListener.SCROLL_STATE_IDLE) {        if (isRefreshing == false) {            isRefreshing = true;            getMoreList();        }    }} 

轉載:http://blog.lytsing.org/archives/322.html

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.