Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android ListView上拉加載的簡單封裝

Android ListView上拉加載的簡單封裝

編輯:關於Android編程

這是對ListView上拉加載的一個簡單封裝

這裡寫圖片描述

繼承自ListView,同時提供加載接口

原理就是通過對FooterView的控制實現上劃加載的效果

public class LoadMoreListView extends ListView {


    private View mLoadMoreFooter;
    private OnLoadMoreListener mLoadMoreListener;
    private boolean mAddedFooter;
    private boolean mLoadMore;


    public interface OnLoadMoreListener extends OnScrollListener {
        // 加載更多數據
        void onLoadMoreData();
    }

    public LoadMoreListView(Context context) {
        super(context);
        init();
    }

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

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

    private void init() {
        mLoadMoreFooter = LayoutInflater.from(getContext()).inflate(R.layout.load_more_view, null);

        this.setOnScrollListener(new OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                if (mLoadMoreListener != null) {
                    mLoadMoreListener.onScrollStateChanged(view, scrollState);
                }
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                if (mLoadMoreListener != null) {
                    mLoadMoreListener.onScroll(view, firstVisibleItem, visibleItemCount, totalItemCount);
                    // 滑動到底部顯示LoadingMore
                    if (firstVisibleItem + visibleItemCount == totalItemCount && mLoadMore) {
                        showLoadMoreView();
                        // 加載耗時操作
                        post(new Runnable() {
                            @Override
                            public void run() {
                                mLoadMoreListener.onLoadMoreData();
                                mLoadMore = false;
                            }
                        });
                    }
                }
            }
        });
    }

    // 加載數據完成
    public void loadMoreComplete() {
        deferNotifyDataSetChanged();
        setLoadMore(false);
    }

    public void setLoadMore(boolean loadMore) {
        mLoadMore = loadMore;
        if (!loadMore) {
            removeFooterView(mLoadMoreFooter);
        }
    }


    private void showLoadMoreView() {
        if (!mAddedFooter) {
            addFooterView(mLoadMoreFooter);
            mAddedFooter = true;
        }
    }

    public void setLoadMoreListener(OnLoadMoreListener loadMoreListener) {
        this.mLoadMoreListener = loadMoreListener;
    }
}

調用示例

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MyListView";

    private LoadMoreListView mLvList;

    private List mData = new ArrayList<>();
    private Handler mHandler = new Handler();

    {
        for (int i = 0; i < 50; i++) {
            mData.add("num " + i);
        }
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mLvList = (LoadMoreListView) findViewById(R.id.id_lv_list);
        final ArrayAdapter adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, mData);
        mLvList.setAdapter(adapter);

        mLvList.setLoadMore(true);

        mLvList.setLoadMoreListener(new LoadMoreListView.OnLoadMoreListener() {
            @Override
            public void onLoadMoreData() {
                // 模擬一個異步操作,阻塞3秒
                new Thread() {
                    @Override
                    public void run() {
                        try {
                            Thread.sleep(3000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        mData.add("LoadMoreOne");
                        mData.add("LoadMoreTwo");
                        mHandler.post(new Runnable() {
                            @Override
                            public void run() {
//                                adapter.notifyDataSetChanged();
                                mLvList.loadMoreComplete();
                            }
                        });
                    }
                }.start();
            }

            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {

            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

            }
        });
    }
}
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved