定义接口
interface OnScrollStateListener {
fun scrollState(isScrolling: Boolean)
}
interface OnScrollPositionListener {
fun scrollPosition(position: Int)
}
interface OnScrollDirectionVerticalListener {
fun scrollUp(dy: Int = 0)
fun scrollDown(dy: Int = 0)
}
interface OnScrollDirectionHorizonListener {
fun scrollLeft(d: Int = 0)
fun scrollRight(dx: Int = 0)
}
定义滑动监听器
class RecycleScrollListener(private var layoutStyle: Int = LAYOUT_STYLE_VERTICAL) :
RecyclerView.OnScrollListener() {
companion object {
const val SCROLL_TOP = 1
const val SCROLL_MIDDLE = 0
const val SCROLL_BOTTOM = -1
const val LAYOUT_STYLE_VERTICAL = 1001
const val LAYOUT_STYLE_HORIZON = 1002
}
private var mOnStateListener: OnScrollStateListener? = null
private var mOnPositionListener: OnScrollPositionListener? = null
private var mOnVerticalListener: OnScrollDirectionVerticalListener? = null
private var mOnHorizonListener: OnScrollDirectionHorizonListener? = null
public fun setOnScrollStateListener(listener: OnScrollStateListener) {
mOnStateListener = listener
}
public fun setOnScrollPositionListener(listener: OnScrollPositionListener) {
mOnPositionListener = listener
}
public fun setOnScrollDirectionVerticalListener(listener: OnScrollDirectionVerticalListener) {
mOnVerticalListener = listener
}
public fun setOnScrollDirectionHorizonListener(listener: OnScrollDirectionHorizonListener) {
mOnHorizonListener = listener
}
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
mOnStateListener?.let {
if (recyclerView.scrollState == 0) {
it.scrollState(false)
} else {
it.scrollState(true)
}
}
mOnPositionListener?.let {
if (layoutStyle == LAYOUT_STYLE_HORIZON) {
if (!recyclerView.canScrollHorizontally(-1)) {
it.scrollPosition(SCROLL_TOP)
} else if (!recyclerView.canScrollHorizontally(1)) {
it.scrollPosition(SCROLL_BOTTOM)
} else {
it.scrollPosition(SCROLL_MIDDLE)
}
} else {
if (!recyclerView.canScrollVertically(-1)) {
it.scrollPosition(SCROLL_TOP)
} else if (!recyclerView.canScrollVertically(1)) {
it.scrollPosition(SCROLL_BOTTOM)
} else {
it.scrollPosition(SCROLL_MIDDLE)
}
}
}
mOnVerticalListener?.let {
if (dy < 0) {
it.scrollUp(dy)
} else if (dy > 0) {
it.scrollDown(dy)
}
}
mOnHorizonListener?.let {
if (dx < 0) {
it.scrollLeft(dx)
} else if (dx > 0) {
it.scrollRight(dx)
}
}
}
}
在Activity中使用监听器
fun initRecyclerView() {
val layoutManager = LinearLayoutManager(this)
layoutManager.orientation = RecyclerView.VERTICAL
val adapter = RecycleViewAdapter()
adapter.setData(getData())
val scrollListener = RecycleScrollListener()
scrollListener?.let {
it.setOnScrollStateListener(object : OnScrollStateListener {
override fun scrollState(isScrolling: Boolean) {
TODO("Not yet implemented")
}
})
it.setOnScrollPositionListener(object : OnScrollPositionListener {
override fun scrollPosition(position: Int) {
when (position) {
RecycleScrollListener.SCROLL_TOP -> Log.i(TAG, "滑动到顶部")
RecycleScrollListener.SCROLL_BOTTOM -> Log.i(TAG, "滑动到底部")
else -> Log.i(TAG, "滑动到非顶部非底部")
}
}
})
it.setOnScrollDirectionVerticalListener(object : OnScrollDirectionVerticalListener {
override fun scrollUp(dy: Int) {
Log.i(TAG, "向上滑动的距离 $dy")
}
override fun scrollDown(dy: Int) {
Log.i(TAG, "向下滑动的距离 $dy")
}
})
}
mRecyclerView?.let {
it.layoutManager = layoutManager
it.adapter = adapter
it.addOnScrollListener(scrollListener)
}
}
|