RecycleView 检测滑动的item位置,以及检测滑动到最后一个item时该做的处理,对于吸顶效果特别需要

/**
rv_find kotlin表示RecycleView的对象
        * 通过滑动监听来判断吸顶效果布局显示不显示
        */
       rv_find.addOnScrollListener(object : RecyclerView.OnScrollListener() {

           override fun onScrolled(recyclerView: RecyclerView?, dx: Int, dy: Int) {
               super.onScrolled(recyclerView, dx, dy)
               Log.i(TAG, "--------------------------------------")
               val layoutManager = recyclerView!!.layoutManager as LinearLayoutManager
               val firstCompletelyVisibleItemPosition = layoutManager.findFirstCompletelyVisibleItemPosition()
               Log.i(TAG, "firstCompletelyVisibleItemPosition: " + firstCompletelyVisibleItemPosition)
               if (firstCompletelyVisibleItemPosition == 0)
                   Log.i(TAG, "滑动到顶部")

               val lastCompletelyVisibleItemPosition = layoutManager.findLastCompletelyVisibleItemPosition()
               Log.i(TAG, "lastCompletelyVisibleItemPosition: " + lastCompletelyVisibleItemPosition)
               if (lastCompletelyVisibleItemPosition == layoutManager.itemCount - 1)
                   Log.i(TAG, "滑动到底部")

               if (lastCompletelyVisibleItemPosition == -1) {
                   include_tab.visibility = View.VISIBLE
               } else {
                   include_tab.visibility = View.INVISIBLE
               }
           }
       })

你可能感兴趣的:(RecycleView 检测滑动的item位置,以及检测滑动到最后一个item时该做的处理,对于吸顶效果特别需要)