RecyclerView vertical automatic infinite rolling, similar to the winning information, running lantern

There are two kinds of effects in RecyclerView:

One is to scroll item as a single item every number of seconds

One is slow rolling for the integral flow form

To scroll indefinitely

This is done by setting itemCount to integer.max_value in adpater

Note: This is done based on the BaseQuickAdapter library, or you can use native

class MainAdatper(data: List<String>) : BaseQuickAdapter<String, BaseViewHolder>(R.layout.item_txt, data) {

    override fun convert(helper: BaseViewHolder? , item:String?).{ helper? .setText(R.id.mTv, item) }override fun getItem(position: Int): String? {
        val newPosition = position % data.size
        return getData().get(newPosition)
    }

    override fun getItemViewType(position: Int): Int {
        var count = getHeaderLayoutCount() + getData().size
        // Count is 0 at the beginning of an activity containing this class. It's going to be 0 percent 0, and that's going to throw an exception, so we're going to make a little bit of a judgment here
        if (count <= 0) {
            count = 1
        }
        var newPosition = position % count;
        return super.getItemViewType(newPosition);
    }

    override fun getItemCount(a): Int {
        return Integer.MAX_VALUE
    }

}
Copy the code

Item is the unit, and one item is rolled every n seconds

Here is mainly used in recyclerView smoothScrollToPosition() method to achieve, and then with custom layoutManager control speed to achieve the goal effect

var layoutManager = object : LinearLayoutManager(this) {
    override fun smoothScrollToPosition(recyclerView: RecyclerView? , state:RecyclerView.State? , position:Int) {
        var linearSmoothScroller = object: LinearSmoothScroller(recyclerView? .context) {// Returns how many milliseconds it takes to slide a Pixel
            override fun calculateSpeedPerPixel(displayMetrics: DisplayMetrics?).: Float {
                // This side can be customized to control speed
                return 3f / (displayMetrics? .density ?:1f)
            }
        }
        linearSmoothScroller.setTargetPosition(position)
        startSmoothScroll(linearSmoothScroller)
    }
}

mRv.layoutManager = layoutManager
mRv.addItemDecoration(DividerItemDecoration(this, DividerItemDecoration.VERTICAL))
mRv.adapter = MainAdatper(data) = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =fun startAuto(a) {
    if(mAutoTask ! =null&& (mAutoTask? .isDisposed ? :true)) mAutoTask? .dispose()// 4 is the last item currently displayed
    mAutoTask = Observable.interval(1.2, TimeUnit.SECONDS).subscribe {
        mRv.smoothScrollToPosition((4 + it).toInt())
    }
}

fun stopAuto(a) {
    if(mAutoTask ! =null&& (mAutoTask? .isDisposed ? :true)) { mAutoTask? .dispose() mAutoTask =null}}override fun onStart(a) {
    super.onStart()
    // Start scrolling
    startAuto()
}

override fun onStop(a) {
    super.onStop()
    // Stop scrolling
    stopAuto()
}

Copy the code

Streaming rolling

Flow scrolling is achieved by using the recyclerView smoothScrollBy(x, Y) method

Here is through the custom recyclerView to achieve, can also be directly implemented in the activity.

class AutoScrollRecyclerView(mContext: Context, attrs: AttributeSet?) : RecyclerView(mContext, attrs) {

    private var mAutoTask: Disposable? = null

    // Disable manual sliding
    override fun onTouchEvent(e: MotionEvent): Boolean {
        return true
    }

    fun start(a) {
        if(mAutoTask ! =null&&! mAutoTask!! .isDisposed) { mAutoTask!! .dispose() } mAutoTask = Observable.interval(1000.100, TimeUnit.MILLISECONDS).observeOn(AndroidSchedulers.mainThread()).subscribe { smoothScrollBy(0.20)}}fun stop(a) {
        if(mAutoTask ! =null&&! mAutoTask!! .isDisposed) { mAutoTask!! .dispose() mAutoTask =null}}} = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =// Call start and stop scrolling in the activity
override fun onStart(a) {
    super.onStart()
    mRvTwo.start()
}

override fun onStop(a) {
    super.onStop()
    mRvTwo.stop()
}
    
Copy the code

The project address

  • AutoScrollRecyclerView