Теперь без хаков невозможно. Я создаю запрос объекта https://issuetracker.google.com/issues/150232913.
И в ожидании функций я создаю метод расширения для установки настраиваемого менеджера макета
package androidx.viewpager2.widget
import androidx.recyclerview.widget.RecyclerView
import java.lang.reflect.Field
fun ViewPager2.setLayoutManager(layoutManager: RecyclerView.LayoutManager) {
mRecyclerView.layoutManager = layoutManager
mRecyclerView.clearOnChildAttachStateChangeListeners() // to ability set exact view size
setValueToField("mLayoutManager", layoutManager)
mScrollEventAdapter.setValueToField("mLayoutManager", layoutManager) // to correct work ViewPager2.OnPageChangeCallback
}
private fun Any.setValueToField(field: String, value: Any) {
val f1: Field = this.javaClass.getDeclaredField(field)
f1.isAccessible = true
f1.set(this, value)
f1.isAccessible = false
}
и создания настраиваемого LayoutManager
class WidePageLayoutManager(
private val viewPager: ViewPager2,
private val viewSmallPercentage: Float,
private val smallViewPosition: IntArray
) : LinearLayoutManager(viewPager.context, HORIZONTAL, false) {
// copied from ViewPager2.LinearLayoutManagerImp
override fun calculateExtraLayoutSpace(state: RecyclerView.State, extraLayoutSpace: IntArray) {
val pageLimit: Int = viewPager.offscreenPageLimit
val offscreenSpace: Int = viewPager.pageSize * pageLimit
extraLayoutSpace[0] = offscreenSpace
extraLayoutSpace[1] = offscreenSpace
}
// copied from ViewPager2.LinearLayoutManagerImp
override fun requestChildRectangleOnScreen(
parent: RecyclerView,
child: View,
rect: Rect,
immediate: Boolean,
focusedChildVisible: Boolean
): Boolean {
return false // users should use setCurrentItem instead
}
// this need to show MainPage bounds on left/right side menu
override fun checkLayoutParams(lp: RecyclerView.LayoutParams?): Boolean {
if (lp != null && lp.viewAdapterPosition in smallViewPosition) {
lp.width = (width * viewSmallPercentage).toInt()
}
return super.checkLayoutParams(lp)
}
override fun canScrollHorizontally(): Boolean {
return super.canScrollHorizontally() && viewPager.isEnabled
}
}