簡體   English   中英

檢測何時recyclerview到達最高位置

[英]Detect when the recyclerview has reached the top most position

我有用於顯示聊天的recyclerview。 我已經設置了layoutmanager的屬性-stackFromEnd-true,該屬性實際上用於到達recyclerview的底部。 我面臨的問題是我想在recyclerview中實現分頁。 當用戶到達頁面頂部時,應該撥打第二頁網絡電話。 我無法為它創建邏輯。 我正在嘗試使用以下代碼,但沒有用。

if (dy < 0) {
                val visibleItemCount = recyclerView.layoutManager!!.childCount
                val totalItemCount = recyclerView.layoutManager!!.itemCount
                val pastVisibleItems = (recyclerView.layoutManager as LinearLayoutManager).findLastVisibleItemPosition()
                Log.e("&&&", "" + visibleItemCount + " " + pastVisibleItems + " " + totalItemCount);

                if (pastVisibleItems <= 3) {
                    presenter.loadMoreMessages(conversationId)
                }
            }

使用RecyclerView布局getPosition

val recyclerViewLayoutManager = recyclerView.layoutManager
recyclerViewLayoutManager?.let {
    if (it.getPosition == 0) {
        //do your work
    }
}

正如@Taseer Ahmad所建議的那樣,這是正確的答案。 我附上代碼片段。

if (dy < 0) {
                if ((recyclerView.layoutManager as LinearLayoutManager).findFirstCompletelyVisibleItemPosition() == 0) {
                    // load more messages
                }
            }

暫無
暫無

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

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