我想在我的选项卡上制作导航视图,然后单击 3 栏,以便导航视图.我该怎么做?



xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/drawer">
<android.support.design.widget.NavigationView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:menu="@menu/nav_menu"
android:layout_gravity="start"
android:id="@+id/navigation_view"
></android.support.design.widget.NavigationView>
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="@dimen/appbar_padding_top"
android:theme="@style/AppTheme.AppBarOverlay">

<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#d3d3d3"
app:tabSelectedTextColor="#000000"
app:tabTextColor="#000000">

<android.support.design.widget.TabItem
android:id="@+id/tabItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="All Terminal" />

<android.support.design.widget.TabItem
android:id="@+id/tabItem1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Terminal 1" />
<android.support.design.widget.TabItem
android:id="@+id/tabItem2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Terminal 2" />
<android.support.design.widget.TabItem
android:id="@+id/tabItem3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Terminal 3" />

</android.support.design.widget.TabLayout>
</android.support.design.widget.AppBarLayout>

这是我现在在主页上的编码,而不是选项卡 1 2 或 3。我想在您单击侧面导航选项卡将显示的 3 栏时显示导航。如何更改 XML 文件以执行此操作?现在它显示了与我想要的不同的东西。

为此,您需要在java文件中实现逻辑:
在活动或java文件中添加以下代码

final DrawerLayout drawerLayout=findViewById(R.id.drawerLayout);
TabLayout tabLayout = findViewById(R.id.tabs);
final int[] counterTabLast = {0};
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
if(tab.getPosition()==2){ //Position 2 means third tab
counterTabLast[0] = counterTabLast[0] +1;
if(counterTabLast[0] ==3){//
drawerLayout.openDrawer(Gravity.START); //Open Drawer
counterTabLast[0]=0;// Reset counter tab
}
}
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});

最新更新