简体   繁体   中英

Detect when the recyclerview has reached the top most position

I have recyclerview that is used to show the chat. I have set the property of the layoutmanager - stackFromEnd - true which is actually used to reach the bottom of the recyclerview. The issue I am facing is that I want to implement paging in the 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

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

As @Taseer Ahmad has suggested, this is the right answer. I am attaching the code snippet.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM