猿问

使用选项卡和片段处理导航项

我创建了一个导航抽屉并添加了这样的布局


activity_main


 <?xml version="1.0" encoding="utf-8"?>

<android.support.v4.widget.DrawerLayout

    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:id="@+id/drawer_layout"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:fitsSystemWindows="true"

    tools:openDrawer="start">


    <include

        layout="@layout/app_bar_main"

        android:layout_width="match_parent"

        android:layout_height="match_parent" />


    <android.support.design.widget.NavigationView

        android:id="@+id/nav_view"

        android:layout_width="wrap_content"

        android:layout_height="match_parent"

        android:layout_gravity="start"

        android:fitsSystemWindows="true"

        app:headerLayout="@layout/nav_header_main"

        app:menu="@menu/activity_main_drawer" />


</android.support.v4.widget.DrawerLayout>

我得到如下所示的结果: 

上面的代码将FirstSemester()和SecondSemester()片段绑定到所有导航项。这可能不是正确的解决方法,因此,我需要建议和帮助,谢谢。



牛魔王的故事
浏览 125回答 3
3回答

浮云间

其次,如何将AppBar标题更改为选定的Navigation项目的标题?在MainActivity类的onNavigationItemSelected()方法中,您可以执行以下操作:@SuppressWarnings("StatementWithEmptyBody")@Overridepublic boolean onNavigationItemSelected(MenuItem item) {&nbsp; &nbsp; // Handle navigation view item clicks here.&nbsp; &nbsp; int id = item.getItemId();&nbsp; &nbsp; if (id == R.id.year1) {&nbsp; &nbsp; &nbsp; &nbsp; getSupportActionBar().setTitle("Year 1");&nbsp; &nbsp; &nbsp; &nbsp; setupViewPager(viewPager);&nbsp; &nbsp; } else if (id == R.id.year2) {&nbsp; &nbsp; &nbsp; &nbsp; getSupportActionBar().setTitle("Year 2");&nbsp; &nbsp; &nbsp; &nbsp; setupViewPager2(viewPager);&nbsp; &nbsp; } else if (id == R.id.year3) {&nbsp; &nbsp; &nbsp; &nbsp; getSupportActionBar().setTitle("Year 3");&nbsp; &nbsp; &nbsp; &nbsp; setupViewPager3(viewPager);&nbsp; &nbsp; } else if (id == R.id.year4) {&nbsp; &nbsp; &nbsp; &nbsp; getSupportActionBar().setTitle("Year 4");&nbsp; &nbsp; &nbsp; &nbsp; setupViewPager4(viewPager);&nbsp; &nbsp; } else if (id == R.id.year5) {&nbsp; &nbsp; &nbsp; &nbsp; getSupportActionBar().setTitle("Year 5");&nbsp; &nbsp; &nbsp; &nbsp; setupViewPager5(viewPager);&nbsp; &nbsp; } else if (id == R.id.nav_share) {&nbsp; &nbsp; } else if (id == R.id.nav_send) {&nbsp; &nbsp; }&nbsp; &nbsp; DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);&nbsp; &nbsp; drawer.closeDrawer(GravityCompat.START);&nbsp; &nbsp; return true;}关于第一个问题,您正在为导航抽屉中单击的任何项目显示一个viewpager(第一和第二个学期),因此您不应期望外观正常。粗略的方法,但是效率不是太差。
随时随地看视频慕课网APP

相关分类

Java
我要回答