androidandroid-recyclerviewandroid-paginglinearlayoutmanager

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

Solution

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