如何在TabLayout中设置当前Tab? - Android?

时间:2018-04-11 13:52:53

标签: java android android-layout android-studio android-fragments

当我使用TabLayout创建Viewpager时,默认情况下,当我启动应用时,我的第一个标签会被选中。

我想要的是,当我启动应用程序时,将选择“否”选项卡,只有当我单击一个时,才会选中它。

有没有办法,怎么实现呢?

编辑:
我有2个tablayouts和2个viewpager用于屏幕

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/rl"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context=".ManagementActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <android.support.design.widget.TabLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/slidingTabs"
            app:tabMaxWidth="0dp"
            app:tabTextAppearance="@android:style/TextAppearance.Widget.TabWidget"
            app:tabGravity="fill"
            app:tabMode="fixed" />

        <android.support.design.widget.TabLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/slidingTabs_2"
            app:tabMaxWidth="0dp"
            app:tabTextAppearance="@android:style/TextAppearance.Widget.TabWidget"
            app:tabGravity="fill"
            app:tabMode="fixed" />

        <android.support.v4.view.ViewPager
            android:id="@+id/viewPager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@android:color/white" />

        <android.support.v4.view.ViewPager
            android:id="@+id/viewPager_2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@android:color/white" />

    </LinearLayout>

</RelativeLayout>

以下是代码:

@Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_management);

        //create tablayout for the first row
        ViewPager viewPager_1 = (ViewPager) findViewById(R.id.viewPager);
        ManagementPagerAdapter adapter_1 = new ManagementPagerAdapter(this, getSupportFragmentManager(), true);
        viewPager_1.setAdapter(adapter_1);

        TabLayout tabLayout_1 = (TabLayout) findViewById(R.id.slidingTabs);
        tabLayout_1.setupWithViewPager(viewPager_1);


        //create tablayout for the second row
        ViewPager viewPager_2 = (ViewPager) findViewById(R.id.viewPager_2);
        ManagementPagerAdapter adapter_2 = new ManagementPagerAdapter(this, getSupportFragmentManager(), false);
        viewPager_2.setAdapter(adapter_2);

        TabLayout tabLayout_2 = (TabLayout) findViewById(R.id.slidingTabs_2);
        tabLayout_2.setupWithViewPager(viewPager_2);

我想要2行标签,但是当我开始时,只会选择一列,现在将选择2列,我不想要它。

0 个答案:

没有答案