ViewPager2 видит часть следующей страницы и центрирует первую и последнюю страницы экрана

У меня был настроен RecyclerView с различными функциями расширения, чтобы центрировать и видеть следующий элемент:

fun RecyclerView.pageSnap() = this.post {
    if (this.onFlingListener == null) PagerSnapHelper().attachToRecyclerView(this)
}

fun RecyclerView.snapCenterWithMargin(left: Int, top: Int, right: Int, bottom: Int) {
    val offsetItemDecoration = SnapCenterWithMargin(left, top, right, bottom)
    this.addItemDecoration(offsetItemDecoration)
}

private class SnapCenterWithMargin(
    private val left: Int,
    private val top: Int,
    private val right: Int,
    private val bottom: Int
) : RecyclerView.ItemDecoration() {

    override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
        view.measure(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)
        val position = parent.getChildAdapterPosition(view)
        val firstItem = 0
        val lastItem = state.itemCount - 1
        when (position) {
            firstItem -> {
                val firstPadding = (parent.width) / 2 - view.measuredWidth / 2
                outRect.set(firstPadding, 0, 0, 0)
            }
            lastItem -> {
                val lastPadding = (parent.width) / 2 - view.measuredWidth / 2
                outRect.set(0, 0, lastPadding, 0)
            }
            else -> {
                outRect.set(left, top, right, bottom)
            }
        }
    }

}

Проблема в том, что анимация ListAdapter при таком подходе работает плохо. Затем я попытался перейти к ViewPager2, но я не могу добиться такого поведения (см. следующий экран и держите элементы по центру).


person JavierSegoviaCordoba    schedule 05.06.2019    source источник
comment
Я не понимаю, чего вы пытаетесь достичь. Я объяснил, как вы можете показать предыдущий и следующий элемент на ViewPager2 здесь: stackoverflow.com/a/58191654/4034572. Это то, что вы хотите?   -  person Albert Vila Calvo    schedule 01.10.2019
comment
Как я могу узнать, если этот пост для просмотра 2, если есть 0 упоминаний о нем как о просмотре 2   -  person JavierSegoviaCordoba    schedule 01.10.2019