簡體   English   中英

RecyclerView 可見計數給出了 Recyclerview 中的總項目數

[英]RecyclerView visible count is giving the total items in Recyclerview

我正在使用 RecyclerView 來顯示項目列表。 總項目數是 10,但一次可見是 3。但是當我使用 recyclerView.getChildCount() 方法獲取可見計數時,它給了我 10,而不是 3。可能是什么問題。 我正在嘗試實現分頁。 因此,每次我的可見計數與 totalitemcount 相同時,結果我的 load more 會不斷被調用,直到所有頁面都加載完畢。 下面是我的代碼。

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

    if (dy > 0) {
        onScrolled();
    }



    visibleItemCount = recyclerView.getChildCount();
    totalItemCount = mLinearLayoutManager.getItemCount();
    firstVisibleItem = mLinearLayoutManager.findFirstVisibleItemPosition();

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

        // Do something
        current_page++;

        onLoadMoreData(current_page);

        loading = true;
    }

}

檢查此答案: 在 RecyclerView 中獲取可見項目


僅根據該答案,您可以執行以下操作:

 LinearLayoutManager layoutManager = ((LinearLayoutManager)mRecyclerView.getLayoutManager()); int visibleItemCount = layoutManager.findLastVisibleItemPosition() - layoutManager.findFirstVisibleItemPosition();

希望它有幫助。

To get the number of items visible on the screen of the recycler view.

rvBreakingNews.addOnScrollListener(object : RecyclerView.OnScrollListener() {
            override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                super.onScrolled(recyclerView, dx, dy)

                val mLayoutManger = recyclerView.layoutManager as LinearLayoutManager
                //To get the total Number of items visible on the screen
                val visibleItemCount = mLayoutManger.childCount 
              //To get the total items loaded in the RecyclerView
                val totalItemCount = mLayoutManger.itemCount 

        })

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM