Android - 初心者 - ListView - 分頁

Android - 初心者 - ListView - 分頁

通常ListView 不會一下呈現大量的資料,有時候下滑到一定的資料後,會再繼續加載新的頁面資料,

假設第一頁是100筆,當滑到第100筆,才會繼續下載新的100筆。

這就是ListView的分頁功能。

參考網路文章

https://blog.csdn.net/a123demi/article/details/41021513

實作最簡易的版本,如下

Activity 先實作 

AbsListView.OnScrollListener

這是ListView要實作覆寫的兩個方法,好判斷是否已滑到最下面

    /**
         *   ListView 分頁功能
         */
    //底部View
    private View footerView;
    //ListView item个数
    int totalItemCount = 0;
    //最后可见的Item
    int lastVisibleItem = 0;
    //是否加载标示
    boolean isLoading = false;
    private int mPage = 1;
    private int mPageSize = 100;
    private boolean mPageEnd = false;

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        //当滑动到底端,并滑动状态为 not scrolling
        if(lastVisibleItem == totalItemCount && scrollState == SCROLL_STATE_IDLE) {
            if (!isLoading) {
                isLoading = true;
                //设置可见
                footerView.setVisibility(View.VISIBLE);
                //加载数据
                if(!mPageEnd) {
                    mPage++;
                    loadDataPage(mPage);
                }
            }
        }
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        this.lastVisibleItem = firstVisibleItem + visibleItemCount;
        this.totalItemCount = totalItemCount;
    }

並且ListView要設定方法與FooterView

        mAdapter = new MemberListAdapter(this);
        mListView.setAdapter(mAdapter);
        mListView.setOnScrollListener(this);
        LayoutInflater mInflater = LayoutInflater.from(this);
        footerView = mInflater.inflate(R.layout.layout_list_footer,null);
        footerView.setVisibility(View.GONE);
        mListView.addFooterView(footerView);

        mPage = 1;
        mPageEnd = false;
        loadDataPage(mPage);
        isLoading = false;
        mListView.setSelection(0);

loadDataPage(int page)則撰寫如下

    public void loadDataPage(int page){
        mPage = page;
        MemberDao dao = MemberDao .getInstance(this);
        List<Member> list = dao.getAll(mPage,mPageSize);

        if(list.size()<mPageSize) {
            mPageEnd = true;
        }

        mAdapter.addAll(list);
        mAdapter.notifyDataSetChanged();
        footerView.setVisibility(View.GONE);
        isLoading = false;
    }

再來建立FooterView Layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout_footer"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="horizontal" >
    <ProgressBar
        style="?android:attr/progressBarStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="30dp"
        android:layout_marginTop="30dp" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:text="正在加载..."
        android:textColor="#0000FF" />
</LinearLayout>

應該這樣,就可以做好分頁功能