首页 > 解决方案 > 是否有任何库可以让我们在 recyclerview 中获得选取框效果(例如 hotstar vip 页面滚动图像)?

问题描述

块引用

我正在尝试实现像hotstar这样的自动滚动无限recyclerview,在hotstar VIP订阅页面中有它。

我试过给定的代码。

对于自动滚动回收器:-

binding.rvPartyOfWeek.addOnScrollListener(CustomScrollListener())

private val SCROLLING_RUNNABLE = object : Runnable {
    override fun run() {
        val duration = 10
        val pixelsToMove = 22

        if (!isPartyOfWeekScrolling) {
            binding.rvPartyOfWeek.smoothScrollBy(pixelsToMove, 0)
        }
        mHandler.postDelayed(this, duration.toLong())
    }
}

对于无限滚动:-

binding.rvPartyOfWeek.addOnScrollListener(object : RecyclerView.OnScrollListener() {
            override fun onScrolled(recyclerView2: RecyclerView, dx: Int, dy: Int) {
                super.onScrolled(recyclerView2, dx, dy)
                val totalItemCount = layoutManagerparty.itemCount
                val lastVisibleItem = layoutManagerparty.findLastVisibleItemPosition()
                if (totalItemCount <= (lastVisibleItem + 3)) {
                    if (totalItemCount > 22) {
                        for (i in 0..10) {
                            listParty.removeAt(0)
                        }
                    }
                    listParty.addAll(listPartySingle)
                    adapterpartyofweek.notifyDataSetChanged()
                    Log.i("Helllo listParty", listParty.size.toString())
                }
            }
        })

它在某些设备中滚动不流畅,在某些旧设备中崩溃。

标签: javaandroidandroid-recyclerviewinfinite-scrollmarquee

解决方案


我做了如下:

  1. 为 RecyclerView 创建自动滚动

`

private fun autoScroll() {
        scrollCount = 0;
        var speedScroll: Long = 1200;
        val runnable = object : Runnable {
            override fun run() {
                if (layoutManager.findFirstVisibleItemPosition() >= imageArrayList.size / 2) {
                    adapter.load()
                }
                recyclerView.smoothScrollToPosition(scrollCount++)
                Log.e(TAG, "run: $scrollCount")
                handler.postDelayed(this, speedScroll)
            }
        }
        handler.postDelayed(runnable, speedScroll)
    }

`

  1. 自动创建平滑滚动

`

layoutManager = object : LinearLayoutManager(this@MainActivity) {
            override fun smoothScrollToPosition(recyclerView: RecyclerView, state: RecyclerView.State?, position: Int) {
                val smoothScroller = object : LinearSmoothScroller(this@MainActivity) {
                    override fun calculateSpeedPerPixel(displayMetrics: DisplayMetrics?): Float {
                        return 5.0f;
                    }
                }
                smoothScroller.targetPosition = position
                startSmoothScroll(smoothScroller)
            }
        }

`

有关源代码,请查看 GitHub 项目链接 https://github.com/Mahesh2318/AutoScrollRecyclerView


推荐阅读