Pagination not work for the RecyclerView within NestedScrollView

前端 未结 3 1509
时光说笑
时光说笑 2020-11-28 09:58

How to implement pagination of recyclerview that is within NestedScrollView?

3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-28 10:38

    I could get the solution setting OnScrollChangeListener in the nestedScrollView.

    The field isLoading should be changed everytime you load the items, for example if you are using retrofit. You could set it as true before It start running and as false when you get the response or the failure.

    The field isLastPage should be changed everytime you get items and check if this page was the last one.

    I'm using kotlin.

    private var isLoading = false
    
    private var isLastPage = false
    
    nestedScrollView.setOnScrollChangeListener { v: NestedScrollView?, scrollX: Int, scrollY: Int, oldScrollX: Int, oldScrollY: Int ->
    
                val nestedScrollView = checkNotNull(v){
                    return@setOnScrollChangeListener
                }
    
                val lastChild = nestedScrollView.getChildAt(nestedScrollView.childCount - 1)
    
                if (lastChild != null) {
    
                    if ((scrollY >= (lastChild.measuredHeight - nestedScrollView.measuredHeight)) && scrollY > oldScrollY && !isLoading && !isLastPage) {
    
                        //get more items
                    }
                }
            }
    

    And of course you need to set the field isNestedScrollingEnabled as false

    myRecyclerView.isNestedScrollingEnabled = false
    

提交回复
热议问题