我正在寻找使用RecyclerViewPagerSnapHelper在项目之间添加空格的方法。
为了证明我的意思,这里有一个gif:
android - 使用PagerSnapHelper在RecyclerView中添加空间-LMLPHP
如上图所示,在两幅图像之间有一个小的黑色空间。但这些空间只能在滚动条上使用。如果scrollState位于SCROLL_STATE_IDLE中,则图像将被居中捕捉并为“全宽”。
这就是我想要实现的。
我的想法是在我的viewType中添加一个新的“占位符”RecyclerView.Adapter,并将PagerSnapHelper自定义为“跳过”占位符viewType(也可以每秒跳过View)。
但不知怎么的,这不管用。我无法将PagerSnaperHelper(或SnapHelper中有大量重复代码的PagerSnapHelper)更改为我想要的工作方式。更进一步地说,在实施过程中,感觉有点不对劲。感觉“这不是应该的方式”。
所以-有什么建议或想法我可以解决这个问题吗?
我完全接受所有的建议。
光盘机:
我知道我可以用旧的ViewPager来做这个。但出于特殊原因,我要求实现一个RecyclerView。所以请不要使用这里的ViewPager,谢谢。

最佳答案

你可以试试这个:
创建两个viewType,第一个是图像,第二个是黑色缩进。然后在SCROLL_STATE_IDLESCROLL_STATE_SETTLING调用smoothScrollToPosition(position)到所需位置。

val pagerSnapHelper = PagerSnapHelper()
val spacePagerSnapHelper = SpacePagerSnapHelper(pagerSnapHelper)
pagerSnapHelper.attachToRecyclerView(recyclerView)
spacePagerSnapHelper.attachToRecyclerView(recyclerView)

这是SpacePagerSnapHelper
/**
 * This is an [RecyclerView.OnScrollListener] which will scrolls
 * the [RecyclerView] to the next closes position if the
 * position of the snapped View (calculated by the given [snapHelper] in [SnapHelper.findSnapView])
 * is odd. Because each odd position is a space/placeholder where we want to jump over.
 *
 * See also [this SO question](https://stackoverflow.com/q/51747104).
 */
class SpacePagerSnapHelper(
        private val snapHelper: SnapHelper
) : RecyclerView.OnScrollListener() {

    private enum class ScrollDirection {
        UNKNOWN, LEFT, RIGHT
    }

    private var scrollDirection: ScrollDirection = UNKNOWN

    fun attachToRecyclerView(recyclerView: RecyclerView) {
        recyclerView.addOnScrollListener(this)
    }

    override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
        scrollDirection = if (dx > 0) RIGHT else LEFT
    }

    override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
        when (newState) {
            RecyclerView.SCROLL_STATE_IDLE -> onPageChanged(recyclerView)
            RecyclerView.SCROLL_STATE_SETTLING -> onPageChanged(recyclerView)
        }
    }

    private fun onPageChanged(recyclerView: RecyclerView) {
        val layoutManager = recyclerView.layoutManager
        val viewToSnap = snapHelper.findSnapView(layoutManager)
        viewToSnap?.let {
            val position = layoutManager.getPosition(it)
            // Only "jump over" each second item because it is a space/placeholder
            // see also MediaContentAdapter.
            if (position % 2 != 0) {
                when (scrollDirection) {
                    LEFT -> {
                        recyclerView.smoothScrollToPosition(position - 1)
                    }
                    RIGHT -> {
                        recyclerView.smoothScrollToPosition(position + 1)
                    }
                    UNKNOWN -> {
                        Timber.i("Unknown scrollDirection... Don't know where to go")
                    }
                }
            }
        }
    }
}

07-28 01:35