android無限ループマルチキャストを実現

2329 ワード

一丶くだらないことを言わないで、直接図の効果を見ます
二丶コアはrecyclerviewであり、それ自体がsmoothScrollToPosition()が指定された位置にスライドしているが、このプロセスは速く、私たちが望んでいる効果ではなく、この方法の中で最終的に呼び出されたのはLinearLayoutManagerのsmoothScrollToPosition()メソッドであるため、LinearLayoutManagerのsmoothScrollToPosition()メソッドを書き換えてスライド速度を制御し、次にコードを上げる必要がある.
import android.content.Context
import android.graphics.PointF
import android.util.DisplayMetrics
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.LinearSmoothScroller
import androidx.recyclerview.widget.RecyclerView

class AutoScrollLayoutManager(context: Context?) : LinearLayoutManager(context) {
    //          
    override fun smoothScrollToPosition(recyclerView: RecyclerView, state: RecyclerView.State, position: Int) {
        val linearSmoothScroller: LinearSmoothScroller = object : LinearSmoothScroller(recyclerView.context) {
            override fun computeScrollVectorForPosition(targetPosition: Int): PointF? {
                return [email protected](targetPosition)
            }

            override fun calculateSpeedPerPixel(displayMetrics: DisplayMetrics): Float {
                //              ,         ;
                return 40f / displayMetrics.density
            }
        }
        linearSmoothScroller.targetPosition = position
        startSmoothScroll(linearSmoothScroller)
    }
}

三丶ずっと転がっていくためにadapterでitemCountをInt.MAX_に戻す必要があるVALUE:
override fun getItemCount(): Int {
            return if (mDataList.isNotEmpty()) Int.MAX_VALUE else 0
        }

三丶通常のLinearLayoutManagerのようにRecyclerviewに設定し、RecyclerviewにAdapterを設定してRecyclerviewを呼び出す.smoothScrollToPosition(Int.MAX_VALUE)では、赤文字を見るとここがIntの最大値、つまりスクロールし続けると問題になるので、データは限られているので、データループを実現する必要があります.adapterのonBindViewHolder(holder:RecyclerView.ViewHolder,position:Int)メソッドでは、カスタムindexで次の位置のデータオブジェクトを取得し、indexがデータの数の長さになるたびにループを再開する必要があることを説明しています.だからindexを0に再設定し、コードは以下の通りです.
 var index = 0
 override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
                    if (index % (mDataList.size - 1) == 0){
                        //           
                        index = 0
                    }
                    val horizontalGame = mDataList[index++]
}