Detect when RecyclerView reaches the bottom most position while scrolling

后端 未结 11 1866
没有蜡笔的小新
没有蜡笔的小新 2020-11-29 19:56

I have this code for a RecyclerView.

    recyclerView = (RecyclerView)rootview.findViewById(R.id.fabric_recyclerView);
    recyclerView.setLayoutManager(layo         


        
11条回答
  •  北海茫月
    2020-11-29 20:29

    I was also searching for this question but I didn't find the answer that satisfied me, so I create own realization of recyclerView.

    other solutions is less precise then mine. for example: if the last item is pretty big (lot of text) then callback of other solutions will come much earlier then recyclerView realy reached bottom.

    my sollution fix this issue.

    class CustomRecyclerView: RecyclerView{
    
        abstract class TopAndBottomListener{
            open fun onBottomNow(onBottomNow:Boolean){}
            open fun onTopNow(onTopNow:Boolean){}
        }
    
    
        constructor(c:Context):this(c, null)
        constructor(c:Context, attr:AttributeSet?):super(c, attr, 0)
        constructor(c:Context, attr:AttributeSet?, defStyle:Int):super(c, attr, defStyle)
    
    
        private var linearLayoutManager:LinearLayoutManager? = null
        private var topAndBottomListener:TopAndBottomListener? = null
        private var onBottomNow = false
        private var onTopNow = false
        private var onBottomTopScrollListener:RecyclerView.OnScrollListener? = null
    
    
        fun setTopAndBottomListener(l:TopAndBottomListener?){
            if (l != null){
                checkLayoutManager()
    
                onBottomTopScrollListener = createBottomAndTopScrollListener()
                addOnScrollListener(onBottomTopScrollListener)
                topAndBottomListener = l
            } else {
                removeOnScrollListener(onBottomTopScrollListener)
                topAndBottomListener = null
            }
        }
    
        private fun createBottomAndTopScrollListener() = object :RecyclerView.OnScrollListener(){
            override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                checkOnTop()
                checkOnBottom()
            }
        }
    
        private fun checkOnTop(){
            val firstVisible = linearLayoutManager!!.findFirstCompletelyVisibleItemPosition()
            if(firstVisible == 0 || firstVisible == -1 && !canScrollToTop()){
                if (!onTopNow) {
                    onTopNow = true
                    topAndBottomListener?.onTopNow(true)
                }
            } else if (onTopNow){
                onTopNow = false
                topAndBottomListener?.onTopNow(false)
            }
        }
    
        private fun checkOnBottom(){
            var lastVisible = linearLayoutManager!!.findLastCompletelyVisibleItemPosition()
            val size = linearLayoutManager!!.itemCount - 1
            if(lastVisible == size || lastVisible == -1 && !canScrollToBottom()){
                if (!onBottomNow){
                    onBottomNow = true
                    topAndBottomListener?.onBottomNow(true)
                }
            } else if(onBottomNow){
                onBottomNow = false
                topAndBottomListener?.onBottomNow(false)
            }
        }
    
    
        private fun checkLayoutManager(){
            if (layoutManager is LinearLayoutManager)
                linearLayoutManager = layoutManager as LinearLayoutManager
            else
                throw Exception("for using this listener, please set LinearLayoutManager")
        }
    
        private fun canScrollToTop():Boolean = canScrollVertically(-1)
        private fun canScrollToBottom():Boolean = canScrollVertically(1)
    }
    

    then in your activity/fragment:

    override fun onCreate() {
        customRecyclerView.layoutManager = LinearLayoutManager(context)
    }
    
    override fun onResume() {
        super.onResume()
        customRecyclerView.setTopAndBottomListener(this)
    }
    
    override fun onStop() {
        super.onStop()
        customRecyclerView.setTopAndBottomListener(null)
    }
    

    hope it will hepl someone ;-)

提交回复
热议问题