Android TabLayout阻止激活与选定项相邻的选项卡 [英] Android TabLayout prevent activation of tabs adjacent to selected

查看:114
本文介绍了Android TabLayout阻止激活与选定项相邻的选项卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基于标签的应用程序.我正在为我的选项卡使用一个TabLayout,并为FragmentStatePagerAdapter的子类实例化所选选项卡的片段.我已禁止在标签之间滑动.我仍然看到一个回调,用于为与所选标签相邻的标签创建片段.换句话说,如果索引0的选项卡被激活,我还将看到索引1的选项卡的GetItem回调.

I have a tab-based app. I am using A TabLayout for my tabs and a subclass of FragmentStatePagerAdapter to instantiate the fragment for the selected tab. I have disabled swiping between tabs. I am still seeing a callback to create a fragment for tabs adjacent to the selected tab. In other words, if the tab at index 0 is activated, I also see a callback to GetItem for the tab at index 1.

我想禁用该行为.换句话说,它只应为活动选项卡请求一个片段.有可能吗?

I want to disable that behaviour. In other words, it should only request a fragment for the active tab. Is that possible?

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/root">
    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabPaddingStart="0dp"
        app:tabPaddingEnd="0dp"
        app:tabPaddingTop="0dp"
        app:tabMode="fixed"
        app:tabGravity="fill" />
    <jockusch.calculator.droid.AndroidViewPager
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

推荐答案

发生这种情况的原因是由于您使用ViewPager.

The reason this is happening is due to you using a ViewPager.

ViewPager默认情况下将自动创建相邻的Fragment,因为这使用户可以在需要时向其滑动.仅在准备好滑动时才创建它们,可能会导致延迟或某些令人不快的视觉效果.

ViewPager, by default, will auto-create the adjacent Fragments because this allows the user to swipe to them whenever they wish. Creating them only when they're ready to swipe might cause lag or some unpleasant visual effects.

ViewPager有一个称为setOffscreenPageLimit(int limit)的方法,该方法可以限制它在内存中保留的相邻数量,但是由于ViewPager的固有行为,我认为无法将其减小为0.

ViewPager has a method called setOffscreenPageLimit(int limit) which can limit the amount of adjacent it keeps in memory, however I believe it's not possible to decrease it to 0 due to ViewPager's innate behavior.

如果您已经禁用了在选项卡之间滑动,则听起来像您想要的不是ViewPager.考虑只使用FragmentTransactions替换活动的Fragment.

If you've already disabled swiping between tabs, then it sounds like what you want isn't a ViewPager. Consider just using FragmentTransactions to replace the active Fragment.

这篇关于Android TabLayout阻止激活与选定项相邻的选项卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆