简体   繁体   English

检测何时recyclerview到达最高位置

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

I have recyclerview that is used to show the chat. 我有用于显示聊天的recyclerview。 I have set the property of the layoutmanager - stackFromEnd - true which is actually used to reach the bottom of the recyclerview. 我已经设置了layoutmanager的属性-stackFromEnd-true,该属性实际上用于到达recyclerview的底部。 The issue I am facing is that I want to implement paging in the recyclerview. 我面临的问题是我想在recyclerview中实现分页。 When the user reaches the top of the page, it should call second page network call. 当用户到达页面顶部时,应该拨打第二页网络电话。 I am unable to create the logic for the same. 我无法为它创建逻辑。 I am trying to play with the below code but of no use. 我正在尝试使用以下代码,但没有用。

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)
                }
            }

Use RecyclerView layout getPosition 使用RecyclerView布局getPosition

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

As @Taseer Ahmad has suggested, this is the right answer. 正如@Taseer Ahmad所建议的那样,这是正确的答案。 I am attaching the code snippet. 我附上代码片段。

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