0

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)
                }
            }
Raghav Sharma
  • 780
  • 10
  • 18

2 Answers2

1

Use RecyclerView layout getPosition

val recyclerViewLayoutManager = recyclerView.layoutManager
recyclerViewLayoutManager?.let {
    if (it.getPosition == 0) {
        //do your work
    }
}
sasikumar
  • 12,540
  • 3
  • 28
  • 48
0

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
                }
            }
Raghav Sharma
  • 780
  • 10
  • 18