如何将前20个联系人加载到回收站视图,并使用OnScrollListener将更多内容加载到回收站视图

时间:2017-01-06 07:48:29

标签: android android-recyclerview

请我正在开发一个Android联系人应用程序,我需要将前20个电话簿联系人加载到回收站视图,并使用Onscroll监听器在滚动时获取剩余的联系人到回收站视图。

1 个答案:

答案 0 :(得分:0)

首先在Contact.on的Arraylist中添加20项Scroll end添加更多项目并调用adapter.notifyDataSetChanged()。这是检测滚动结束的代码。

mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {

@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
    super.onScrolled(recyclerView, dx, dy);

    visibleItemCount = mRecyclerView.getChildCount();
    totalItemCount = mLayoutManager.getItemCount();
    firstVisibleItem = mLayoutManager.findFirstVisibleItemPosition();

    if (loading) {
        if (totalItemCount > previousTotal) {
            loading = false;
            previousTotal = totalItemCount;
        }
    }
    if (!loading && (totalItemCount - visibleItemCount) 
        <= (firstVisibleItem + visibleThreshold)) {
        // End has been reached

        Log.i("Yaeye!", "end called");

        // Add more items into contact Arraylist and call notifyDataSetChanged()

        loading = true;
    }
}

});

注意:确保使用LinearLayoutManager作为RecyclerView的布局管理器。

LinearLayoutManager mLayoutManager;
mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);