Android實(shí)踐 | ListView簡單封裝,實(shí)現(xiàn)上拉加載

簡單封裝,因此代碼邏輯很簡單,直接結(jié)合注釋看代碼:

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

import com.laughter.framework.OnLoadMoreListener;

public class LoadingListView extends ListView implements AbsListView.OnScrollListener {

    //item總數(shù)
    private int mTotalItemCount;
    // 是否正在加載
    private boolean isLoading;
    // 加載接口 
    private OnLoadMoreListener mLoadingListener;

    // FooterView 需要通過 addFooterView(View v) 方法添加進(jìn)來
    private View mLoadingView;

    public LoadingListView(Context context) {
        this(context, null);
    }

    public LoadingListView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public LoadingListView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        // 取消滑動到邊界的弧形陰影
        setOverScrollMode(OVER_SCROLL_NEVER);
        // 設(shè)置滑動監(jiān)聽
        setOnScrollListener(this);
    }

    // 設(shè)置自定義接口 OnLoadMoreListener
    public void setOnLoadMoreListener(OnLoadMoreListener listener){
        this.mLoadingListener = listener;
    }

    /**
     * 數(shù)據(jù)加載完成后,調(diào)用此方法
     * 將正在加載標(biāo)記置為 false
     * 并且將 FooterView 移除掉
     */
    public void setLoadCompleted() {
        isLoading = false;
        removeFooterView(mLoadingView);
    }

    @Override
    public void onScrollStateChanged(AbsListView listView, int scrollState) {
        // 獲取最后一個可見item的 position
        int lastVisibleIndex = listView.getLastVisiblePosition();
        // 如果當(dāng)前未加載,并且滾動停止,并且最后一個可見item是當(dāng)前l(fā)ist的最后一個
        if (!isLoading && scrollState == SCROLL_STATE_IDLE && lastVisibleIndex == mTotalItemCount-1){
            isLoading = true;
            // 顯示LoadingView ,并且回調(diào) onLoadMore() 方法
            addFooterView(mLoadingView);
            if (mLoadingListener != null){
                mLoadingListener.onLoadMore();
            }
        }
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        mTotalItemCount = totalItemCount;
    }

    @Override
    public void addFooterView(View v) {
        mLoadingView = v;
        super.addFooterView(mLoadingView);
    }
}

下面是 OnLoadMoreListener 接口

public interface OnLoadMoreListener {
    public void onLoadMore();
}

使用也很簡單,先看布局文件:

    <com.laughter.framework.views.LoadingListView
        android:id="@+id/llv_collection"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    public void initView() {    
        mListView.addFooterView(LayoutInflater.from(this)
                      .inflate(R.layout.layout_footer_view, mListView, false));
        mListView.setOnLoadMoreListener(this);
    }

    @Override
    public void onLoadMore() {
          // 這里可以進(jìn)行網(wǎng)絡(luò)請求,數(shù)據(jù)加載完成后,調(diào)用mListView.setLoadCompleted();
    }

下面看一下效果:

之所以不在 LoadingListView 中把LoadingView寫死,是為了方便定制,需要什么樣的LoadingView可以自行定義。下面是我寫的Loadingview:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#F5F6F8"
    android:gravity="center"
    android:padding="8dp">

    <ProgressBar
        android:id="@+id/progress_bar"
        android:layout_width="24dp"
        android:layout_height="24dp"
        android:layout_marginEnd="16dp"
        android:indeterminateTint="@color/colorWeakBlack"/>

    <TextView
        android:id="@+id/tv_hint"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/loading"
        android:textSize="15sp"
        android:textColor="@color/colorWeakBlack"/>

</LinearLayout>

使用實(shí)例可以參考:LoadingListView

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • ¥開啟¥ 【iAPP實(shí)現(xiàn)進(jìn)入界面執(zhí)行逐一顯】 〖2017-08-25 15:22:14〗 《//首先開一個線程,因...
    小菜c閱讀 7,384評論 0 17
  • 1. 簡介 1.1 什么是 MyBatis ? MyBatis 是支持定制化 SQL、存儲過程以及高級映射的優(yōu)秀的...
    笨鳥慢飛閱讀 6,280評論 0 4
  • 這是16年5月份編輯的一份比較雜亂適合自己觀看的學(xué)習(xí)記錄文檔,今天18年5月份再次想寫文章,發(fā)現(xiàn)簡書還為我保存起的...
    Jenaral閱讀 3,172評論 2 9
  • Swift1> Swift和OC的區(qū)別1.1> Swift沒有地址/指針的概念1.2> 泛型1.3> 類型嚴(yán)謹(jǐn) 對...
    cosWriter閱讀 11,684評論 1 32
  • 做過的夢許許多多,斷斷續(xù)續(xù)。 而我恨它。 既然讓我做,為什么不留下? 每次醒來意猶未盡,卻總是斷片,什么鬼??? ...
    林香文閱讀 240評論 0 0

友情鏈接更多精彩內(nèi)容