Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

[Android] 自定義 ListView 實作上拉加載更多

編輯:關於 Android 程式設計

本文所說的「上拉加載」,指的是當 ListView 滾到底部、使用者再繼續往下滑時,底部浮現「正在加載中」的提示,資料到手後提示消失、新項目接續出現。

核心思路:把 footer 當成一個可暫時隱藏的哨兵

ListView 本身就提供 addFooterView 這樣的方法。於是可以反過來想:先在尾端掛一個 footer,當監聽到已經滑到底時把 footer 顯示出來並觸發加載;等資料更新完成、ListView 重新佈局之後,新加入的資料自然會把 footer 擠到看不見的位置。如此循環,就構成了「上拉加載更多」。

依這個思路拆成四步:

a) 建立自定義 ListView
b) 為它加上底部布局
c) 為它加上滾動監聽
d) 讓「滑到底」這個事件能被外部接收並處理

a) 建立 MyPullUpListView

public MyPullUpListView(Context context) {
    super(context);
}

public MyPullUpListView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

b) 底部布局 listview_loadbar.xml

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:background="#00000000"
        android:gravity="center"
        android:text="正在加載中。。。"
        android:textSize="14sp" />
</LinearLayout>

把它掛上去的程式碼:

/** 初始化底部頁面 */
public void initBottomView() {
    if (footerView == null) {
        footerView = LayoutInflater.from(this.context)
                .inflate(R.layout.listview_loadbar, null);
    }
    addFooterView(footerView);
}

c) 實作 OnScrollListener

public void onScrollStateChanged(AbsListView view, int scrollState) {
    // 滑動停止且不在第一項,代表可能已經在底部
    if (scrollState == OnScrollListener.SCROLL_STATE_IDLE && firstVisibleItem != 0) {
        // 觸發加載更多
    }
}

public void onScroll(AbsListView view, int firstVisibleItem,
                     int visibleItemCount, int totalItemCount) {
    this.firstVisibleItem = firstVisibleItem;
    if (footerView != null) {
        // 所有項目一屏就能顯示完,就沒有「更多」可加載
        if (visibleItemCount == totalItemCount) {
            footerView.setVisibility(View.GONE);
        } else {
            footerView.setVisibility(View.VISIBLE);
        }
    }
}

d) 用回呼介面把事件交給業務層

/** 上拉加載更多的回呼監聽 */
public interface MyPullUpListViewCallBack {
    void scrollBottomState();
}

在監聽裡叫它:

public void onScrollStateChanged(AbsListView view, int scrollState) {
    if (scrollState == OnScrollListener.SCROLL_STATE_IDLE && firstVisibleItem != 0) {
        myPullUpListViewCallBack.scrollBottomState();
    }
}

使用方式

myListView = (MyPullUpListView) findViewById(R.id.mylist);
myListView.initBottomView();
myListView.setAdapter(listViewAdapter);
myListView.setMyPullUpListViewCallBack(new MyPullUpListViewCallBack() {
    public void scrollBottomState() {
        // 在這裡發請求、取資料,更新後呼叫 notifyDataSetChanged()
    }
});

完整程式碼

package com.xiaoyan.xiaoyanlibrary.common.widget.listview;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ListView;

/**
 * 上拉加載更多的 ListView
 */
public class MyPullUpListView extends ListView implements OnScrollListener {

    /** 底部顯示「正在加載」的頁面 */
    private View footerView = null;
    private Context context;
    private MyPullUpListViewCallBack myPullUpListViewCallBack;
    /** 記錄第一行 Item 的序號 */
    private int firstVisibleItem;

    public MyPullUpListView(Context context) {
        super(context);
        this.context = context;
        initListView();
    }

    public MyPullUpListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        initListView();
    }

    /** 初始化 ListView */
    private void initListView() {
        setOnScrollListener(this);
        setFooterDividersEnabled(false);   // 去掉 footer 上的分割線
    }

    /** 初始化底部頁面 */
    public void initBottomView() {
        if (footerView == null) {
            footerView = LayoutInflater.from(this.context)
                    .inflate(R.layout.listview_loadbar, null);
        }
        addFooterView(footerView);
    }

    public void onScrollStateChanged(AbsListView view, int scrollState) {
        if (scrollState == OnScrollListener.SCROLL_STATE_IDLE && firstVisibleItem != 0) {
            if (myPullUpListViewCallBack != null) {
                myPullUpListViewCallBack.scrollBottomState();
            }
        }
    }

    public void onScroll(AbsListView view, int firstVisibleItem,
                         int visibleItemCount, int totalItemCount) {
        this.firstVisibleItem = firstVisibleItem;
        if (footerView != null) {
            if (visibleItemCount == totalItemCount) {
                footerView.setVisibility(View.GONE);
            } else {
                footerView.setVisibility(View.VISIBLE);
            }
        }
    }

    public void setMyPullUpListViewCallBack(
            MyPullUpListViewCallBack myPullUpListViewCallBack) {
        this.myPullUpListViewCallBack = myPullUpListViewCallBack;
    }

    /** 上拉加載更多的回呼監聽 */
    public interface MyPullUpListViewCallBack {
        void scrollBottomState();
    }
}

實作上還少了兩塊:加載失敗與「沒有更多資料」的狀態處理,以及加載過程中的防重複觸發旗標。若要產品化,建議改用 SwipeRefreshLayout 搭配 RecyclerView,官方已經把邊界判斷做得比手工版乾淨得多。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved