this
解决方案。
所以我就是这么做的
RecycleServiceAdapterClass类
fun initialize(scrollableData: ArrayList<ScrollableData>, activity: AppCompatActivity) {
this.scrollableData = scrollableData
this.activity = activity
list = /*listOf(scrollableData.last()) +*/ scrollableData + listOf(scrollableData.first()) //this is very important
}
...
override fun getItemViewType(position: Int): Int {
return list[position % list.size].index
}
mainScreenAdapter.itemCount.takeIf { it > 1 }
?.apply {
onScrollListener = OnScrollListener(
mainScreenAdapter.itemCount,
linearLayoutManager
) {
//some code here
}
recycler_view.addOnScrollListener(onScrollListener)
recycler_view.scrollToPosition(0)
}
internal class OnScrollListener(val itemCount: Int, val layoutManager: LinearLayoutManager,
val stateChanged: (Int) -> Unit) : RecyclerView.OnScrollListener() {
var isLooping = false
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
super.onScrolled(recyclerView!!, dx, dy)
val firstItemVisible = layoutManager.findFirstCompletelyVisibleItemPosition()
if (firstItemVisible > 0 && firstItemVisible % (itemCount - 1) == 0) {
// When position reaches end of the list, it sho uld go back to the beginning
isLooping = true
recyclerView?.scrollToPosition(0)
} /*else if (firstItemVisible == 0) {
// When position reaches beginning of the list, it should go back to the end
recyclerView?.scrollToPosition(itemCount-1)
}*/
}
override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
super.onScrollStateChanged(recyclerView!!, newState)
var loop = newState
if (isLooping) {
loop = 100
isLooping = false
}
stateChanged(loop)
}
}
裁判:
https://github.com/tomoima525/CirculerAutoScrollingRecyclerView