ViewPager2 видит часть следующей страницы и центр на экране первой и последней страницы - PullRequest
0 голосов
/ 05 июня 2019

У меня был 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, но у меня не получается это поведение (см. Следующий экран и держите элементы в центре).

...