以编程方式使用选项卡更改片段的选项卡索引 [英] Change tab index of fragments with tabs programmatically

查看:124
本文介绍了以编程方式使用选项卡更改片段的选项卡索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当尝试以编程方式切换片段内的tablayout的选项卡时,出现了空指针异常,

I'm getting a null pointer exception when trying to switch tabs programmatically of a tablayout inside a fragment,

所以我的主要活动有一个选项卡布局(4个选项卡),每个选项卡都有一个包含一个片段的视图分页器,并且每个这些片段都有一个选项卡布局(x数量的选项卡),其中一个视图分页器包含一个片段,我可以从任何类似这样的片段

So I have my main activity that has a tab layout (4 tabs) each tab has a view pager holding a fragment, and each of these fragments has a tab layout (x amount of tabs) with a view pager holding a fragment, i can switch the tabs of my main activity tab layout from any fragment like this

TabLayout tabLayout = MainActivity.tabLayout;
TabLayout.Tab tab = tabLayout.getTabAt(2);
tab.select();

但是如果我尝试以相同的方式更改片段之一的选项卡,则会得到一个空指针

but if i try to change the tabs of one of the fragments in the same way i get a null pointer

TabLayout tabLayout2 = tabFragOne.tabLayout;
TabLayout.Tab tab2 = tabLayout2.getTabAt(2);
tab2.select();

仅当我在应用程序首次打开时单击有问题的按钮时,这种情况才会发生,这表明其原因是尚未附加或创建片段,

it only happens if I click the button in question when the app first opens, which suggests that the reason for it is that the fragment hasn't been attached or created yet,

例如,如果我滚动到我想切换到的片段"选项卡,然后返回到主要活动,然后按一下有问题的按钮,它将起作用.有谁知道解决此问题的最佳方法?

for instance if i scroll across to the fragments tab i want to switch to, and then go back to the main activity and press the button in question it will work. does anyone know the best way to fix this?

好吧,我发现这个问题的症结所在实际上是即时通讯使用了视图寻呼机适配器,这是一个问题

Ok ive found half the crux to this question is actually that im using a view pager adapter, a question here sheds a lot of light on my issue

推荐答案

设置选定标签的正确方法,我们应该改为从Pager设置选定索引.

The correct way to set selected Tab, we should set selected index from Pager instead.

final ViewPager pager = (ViewPager) findViewById(R.id.pager);
final TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);

tabLayout.setupWithViewPager(pager);
pager.setCurrentItem(0);//selected position

这是我在xml中的布局设计

Here is my layout design in xml

<android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="#ffffff"
            android:background="@color/al_white"
            android:elevation="0dp" />

       <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#ffffff"
            app:tabTextColor="#494a43"
            app:tabSelectedTextColor="#494a43"
            app:tabIndicatorColor="#494a43"
            app:tabIndicatorHeight="2dp"
            app:tabMode="fixed"
            app:tabGravity="fill" />
</android.support.design.widget.AppBarLayout>

这篇关于以编程方式使用选项卡更改片段的选项卡索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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