3

我正在寻找一种使用 aRecyclerView和 . 在我的项目之间添加空格的方法PagerSnapHelper

为了证明我的意思是一个 gif:

正如您在上面看到的两个图像之间有一点黑色空间。但是这些空间只能在 scroll 上使用。如果scrollStateSCROLL_STATE_IDLE图像中,则居中对齐并且是“全宽”。

这就是我想要达到的目标。

我的想法是为我添加一个新的“占位符”viewTypeRecyclerView.Adapter并自定义PagerSnapHelper“跳过”占位符viewTypeView(也可以每秒跳过)。但不知何故,这不起作用。我无法更改PagerSnaperHelper(或SnapHelper带有大量重复代码的PagerSnapHelper)以按我的意愿工作。更进一步,在实施它时感觉有些不对劲。感觉就像“这不是应该的方式”。

那么 - 有什么建议或想法可以解决这个问题吗?我完全愿意接受所有建议。

Disclamer:
我知道我可以使用旧ViewPager的。但出于特殊原因,我要求实现RecyclerView. 所以请不要建议在ViewPager这里使用,谢谢。

4

2 回答 2

3

你可以试试这个:创建两个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")
                    }
                }
            }
        }
    }
}
于 2018-08-08T13:37:55.867 回答
2

UgAr0FF 的解决方案通常有效,但也会导致不需要的滚动行为:

它不应该看起来如何

如您所见,PagerSnapHelper 首先尝试关注分隔线,然后决定移动到下一项。我修复了滚动行为,使其看起来像这样:

它应该是什么样子

这是代码:

import androidx.recyclerview.widget.PagerSnapHelper
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.SCROLL_STATE_IDLE
import androidx.recyclerview.widget.RecyclerView.SCROLL_STATE_SETTLING


class PagerSnapAndSkipDividerHelper : PagerSnapHelper() {

    private var dx = 0

    override fun attachToRecyclerView(recyclerView: RecyclerView?) {
        super.attachToRecyclerView(recyclerView)
        recyclerView?.addOnScrollListener(onScrollListener)
    }

    override fun findTargetSnapPosition(
        layoutManager: RecyclerView.LayoutManager?,
        velocityX: Int,
        velocityY: Int
    ): Int = getNextNonDividerPosition(
        super.findTargetSnapPosition(layoutManager, velocityX, velocityY)
    )

    private fun getNextNonDividerPosition(position: Int) =
        if (position % 2 == 0) position else if (dx > 0) position + 1 else position - 1

    private val onScrollListener = object : RecyclerView.OnScrollListener() {
        override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
            this@PagerSnapAndSkipDividerHelper.dx = dx
        }

        override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
            if (listOf(SCROLL_STATE_IDLE, SCROLL_STATE_SETTLING).contains(newState)) {
                val viewToSnapTo = findSnapView(recyclerView.layoutManager) ?: return
                val position = recyclerView.layoutManager?.getPosition(viewToSnapTo) ?: return
                val nonDividerPosition = getNextNonDividerPosition(position)
                if (position != nonDividerPosition) {
                    recyclerView.smoothScrollToPosition(nonDividerPosition)
                }
            }
        }
    }
}

像这样设置它:

PagerSnapAndSkipDividerHelper().attachToRecyclerView(recyclerView)
于 2018-11-08T11:01:30.813 回答