Android:Viewpager2 卡在一半了

问题描述 投票:0回答:1

当尝试在我的任何 viewpager2 片段之间滑动时,视图会卡在过渡的 75% 处。

图片

从第一个片段滑动到第二个片段:

enter image description here

从第二个片段滑动到第一个片段:

enter image description here

Viewpager_布局

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.viewpager2.widget.ViewPager2
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:orientation="horizontal"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <com.google.android.material.tabs.TabLayout
        android:id="@+id/tl_on_boarding_item"
        style="@style/ShapeAppearanceOverlay.EXAMPLE.ShopTablayout"
        android:layout_width="0dp"
        android:layout_height="16dp"
        android:layout_marginBottom="24dp"
        android:background="@android:color/transparent"
        app:layout_constraintBottom_toBottomOf="@+id/viewPager"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:tabBackground="@drawable/unselected_shop_item_tab"
        app:tabIndicator="@drawable/selected_on_boarding_item_tab"
        app:tabIndicatorColor="@color/primary"
        app:tabIndicatorFullWidth="false"
        app:tabIndicatorGravity="center"
        app:tabIndicatorHeight="8dp"
        app:tabMaxWidth="16dp" />

</androidx.constraintlayout.widget.ConstraintLayout>

Viewpager_Fragment

@AndroidEntryPoint
class OnBoardingFragmentHolder @Inject constructor(
    private val onBoardingViewPagerAdapter: OnBoardingViewPagerAdapter,
    private val mediator: TabLayoutHelper,
) : InvisibleBottomNavFragment<FragmentOnBoardingHolderBinding>() {

    override val bindingInflater: (LayoutInflater) -> FragmentOnBoardingHolderBinding
        get() = FragmentOnBoardingHolderBinding::inflate

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initTablayout()
    }

    private fun initTablayout() {
        binding.viewPager.adapter = onBoardingViewPagerAdapter
        binding.viewPager.offscreenPageLimit = 2
        mediator.init(binding.tlOnBoardingItem, binding.viewPager)
    }

    override val onDestroyView: () -> Unit
        get() = {
            binding.viewPager.adapter = null
            mediator.onDestroyView()
        }
}

隐形底部碎片

abstract class InvisibleBottomNavFragment<out T : ViewBinding> : BindingFragment<T>() {
    override fun onAttach(context: Context) {
        super.onAttach(context)
        postStickyEvent(MainActivityBusEventBottomNav(toBeClosed = true,
            fromClass = "InvisibleBottomNavFragment.kt"))
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        postStickyEvent(MainActivityBusEventBottomNav(toBeClosed = true,
            fromClass = "InvisibleBottomNavFragment.kt"))
    }

    override fun onResume() {
        super.onResume()
        postStickyEvent(MainActivityBusEventBottomNav(toBeClosed = true,
            fromClass = "InvisibleBottomNavFragment.kt"))
    }
}

查看页面

class OnBoardingViewPagerAdapter @Inject constructor(
    fragment: Fragment
) : FragmentStateAdapter(fragment) {
    private companion object {
        private const val FRAGMENT_ITEM_COUNT = 3
    }

    override fun getItemCount(): Int = FRAGMENT_ITEM_COUNT

    override fun createFragment(position: Int): Fragment = when (position) {
        0 -> OnBoardingFirstFragment()
        1 -> OnBoardingSecondFragment()
        2 -> OnBoardingThirdFragment()
        else -> throw IllegalStateException("$position can not be satisfied")
    }
}

我觉得最奇怪的是,当我在片段之间快速滑动时(例如,不将鼠标按钮一直按住到最后,只是滑动),一切正常。

android android-viewpager android-viewpager2
1个回答
0
投票

我也遇到过类似的问题。我在选项卡式屏幕上有相当多的输入页面。第一次出现在某个页面时,它卡在了中间。

我通过关闭

smoothScroll

轻松解决了这个问题
        new TabLayoutMediator(tabLayout, viewPager,true, false, tabsAdapter::applyDecoration).attach();

  public TabLayoutMediator(
      @NonNull TabLayout tabLayout,
      @NonNull ViewPager2 viewPager,
      boolean autoRefresh, 
      boolean smoothScroll, //<---- Settung to false do the job
      @NonNull TabConfigurationStrategy tabConfigurationStrategy) 

老实说,它变得更好了。在我看来,这个动画无论如何看起来都很尴尬。

© www.soinside.com 2019 - 2024. All rights reserved.