导航栏无法正常工作

时间:2017-11-09 07:45:11

标签: android navigation-drawer

我创建了一个导航抽屉,当我点击关于的项目时,它会在nav_bar中显示关于但不显示关于它的xml页面。它以xml布局显示主页。 当我点击“关于”时,它会显示我的主页

My nav_bar image

When clicked About_image

After clicking About, it again shows my default layout

我的主要活动

public class MainActivity extends AppCompatActivity {


public DrawerLayout mDrawer;
public Toolbar toolbar;
public NavigationView nvDrawer;

private ActionBarDrawerToggle drawerToggle;

// Button Activity


public Button btn;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    btn = (Button) findViewById(R.id.btn);
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent in = new Intent(MainActivity.this,FiveFragment.class);
            startActivity(in);
        }
    });

    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    mDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    nvDrawer = (NavigationView) findViewById(R.id.nvView);
    setupDrawerContent(nvDrawer);
    drawerToggle = setupDrawerToggle();
    mDrawer.addDrawerListener(drawerToggle);
}

private ActionBarDrawerToggle setupDrawerToggle() {

    // NOTE: Make sure you pass in a valid toolbar reference.  ActionBarDrawToggle() does not require it
    // and will not render the hamburger icon without it.
    return new ActionBarDrawerToggle(this, mDrawer, toolbar, R.string.drawer_open, R.string.drawer_close);

}

@Override
protected void onPostCreate(Bundle savedInstanceState) {

    super.onPostCreate(savedInstanceState);
    // Sync the toggle state after onRestoreInstanceState has occurred.
    drawerToggle.syncState();

}

public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    // Pass any configuration change to the drawer toggles
    drawerToggle.onConfigurationChanged(newConfig);
}

private void setupDrawerContent(NavigationView navigationView) {

    navigationView.setNavigationItemSelectedListener(

            new NavigationView.OnNavigationItemSelectedListener() {

                @Override

                public boolean onNavigationItemSelected(MenuItem menuItem) {

                    selectDrawerItem(menuItem);

                    return true;

                }

            });

}

@Override

public boolean onOptionsItemSelected(MenuItem item) {
    // The action bar home/up action should open or close the drawer.

    switch (item.getItemId()) {
        case android.R.id.home:
            mDrawer.openDrawer(GravityCompat.START);
            return true;

    }
    return super.onOptionsItemSelected(item);

}

public void selectDrawerItem(MenuItem menuItem) {

    // Create a new fragment and specify the fragment to show based on nav item clicked

    android.support.v4.app.Fragment fragment = null;
    Class fragmentClass = null;
    switch (menuItem.getItemId()) {

        case nav_first_fragment:  // Home
            fragmentClass = OneFragment.class;
            break;

        case nav_second_fragment:  // Depertment
            fragmentClass = TwoFragment.class;
            break;

        case nav_third_fragment:  // Resources
            fragmentClass = ThreeFragment.class;
            break;

        case nav_four_fragment: //Contact us
            fragmentClass = FourFragment.class;
            break;

        case nav_five_fragment:  // About
            fragmentClass = FiveFragment.class;
            break;

        case nav_six_fragment:  // Update
            fragmentClass = SixFragment.class;
            break;

        case nav_seven_fragment:  // Share
            fragmentClass = SevenFragment.class;
            break;

         //default:
         //  fragmentClass = OneFragment.class; //Home
         }

    try {
        fragment = (Fragment) fragmentClass.newInstance();
    } catch (Exception e) {
        e.printStackTrace();
    }


        // Insert the fragment by replacing any existing fragment

        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.flContent, fragment).commit();
        // Highlight the selected item has been done by NavigationView
        menuItem.setChecked(true);
        // Set action bar title
        setTitle(menuItem.getTitle());
        // Close the navigation drawer
        mDrawer.closeDrawers();

    } }

我的主要xml页面

xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- This LinearLayout represents the contents of the screen  -->

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <!-- The ActionBar displayed at the top -->

    <include
        layout="@layout/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <!-- The main content view where fragments are loaded -->

    <FrameLayout
        android:id="@+id/flContent"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="5dp"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <TextView
            android:id="@+id/textView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="52dp"
            android:text="@string/About" />
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/btn"
            android:text="@string/ContactUs"
            android:background="@color/colorPrimary"
            android:clickable="true"
            android:padding="3dp"/>
    </FrameLayout>

</LinearLayout>
<!-- The navigation drawer that comes from the left -->

<!-- Note that `android:layout_gravity` needs to be set to 'start' -->

<android.support.design.widget.NavigationView
    android:id="@+id/nvView"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="@android:color/white"
    app:menu="@menu/drawer_view"
    app:itemIconTint="@color/drawer_item"
    app:itemTextColor="@color/drawer_item"
    app:itemBackground="@drawable/drawer_item_bg"
    app:headerLayout="@layout/nav_header">

</android.support.design.widget.NavigationView>

我的关于xml页面

<FrameLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:layout_marginTop="20dp"
android:background="@android:color/holo_red_light"
android:padding="5dp">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    tools:ignore="UselessParent">

    <TextView
        android:text="@string/About"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/about"
        android:layout_marginTop="15dip"
        android:gravity="center"
        android:textColor="@android:color/holo_blue_bright"
        />

    <RelativeLayout
        android:id="@+id/header"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dip"
        android:background="@android:color/white">
        <ImageView
            android:id="@+id/aboutimg"
            android:src="@drawable/logo_splash"
            android:contentDescription="@string/aboutimg"
            android:layout_margin="10dp"
            tools:ignore="RtlHardcoded"
            android:layout_width="85dp"
            android:layout_height="85dp"
            app:srcCompat="@drawable/logo_splash" />
        <TextView
            android:text="@string/rmstu"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/textView5"
            android:layout_toEndOf="@+id/aboutimg"
            android:layout_marginTop="20dp"
            android:textStyle="bold"
            android:textSize="18sp"
            android:textColor="@android:color/holo_red_dark"
            android:layout_marginLeft="10dp"
            tools:ignore="RtlHardcoded"
            tools:text="RMSTU" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/version"
            android:text="@string/version_1_0"
            android:layout_below="@+id/textView5"
            android:layout_toEndOf="@+id/aboutimg"
            android:textStyle="normal"
            android:layout_marginTop="10dp"
            android:textSize="18sp"
            android:layout_marginLeft="10dp"
            tools:ignore="RtlHardcoded"
            android:textColor="@android:color/black"
            tools:text="Version : 1.0" />
    </RelativeLayout>

    <ScrollView
        android:layout_width="match_parent"
        android:id="@+id/scrollViewab1"
        android:layout_marginTop="5dp"
        android:layout_height="200dip">

        <LinearLayout
            android:layout_width="match_parent"
            android:id="@+id/body"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:background="#d8d7d7"
            android:padding="10dip"
            tools:ignore="RtlHardcoded">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/linear1"
                android:layout_marginTop="8dp"
                android:text="This Application is a free application for RMSTU. Your support and suggestions are very important to us to make this application better!"
                tools:ignore="HardcodedText"
                android:textSize="14sp"
                android:contentDescription="aboutdescription1"
                android:textColor="@android:color/black"
                android:padding="3dp"
                android:maxLines="12"
                android:scrollbars="vertical"
                />
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/linear2"
                android:layout_marginTop="8dp"
                android:text="@string/Thanksto"
                tools:ignore="HardcodedText"
                android:textSize="16sp"
                android:contentDescription="aboutdescription1"
                android:gravity="center_horizontal"
                android:textColor="@android:color/black"
                android:textStyle="bold" />
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/linear3"
                android:layout_marginTop="2dp"
                android:text="@string/Faculty_members"
                tools:ignore="HardcodedText"
                android:textSize="14sp"
                android:contentDescription="aboutdescription1"
                android:gravity="center_horizontal"
                android:textColor="@android:color/black"/>
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/linear4"
                android:layout_marginTop="10dp"
                android:text="@string/Specialthanksto"
                tools:ignore="HardcodedText"
                android:textSize="16sp"
                android:contentDescription="aboutdescription1"
                android:gravity="center_horizontal"
                android:textColor="@android:color/black"
                android:textStyle="bold" />
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/linear5"
                android:layout_marginTop="5dp"
                android:text="@string/juelshikder"
                tools:ignore="HardcodedText"
                android:textSize="16sp"
                android:contentDescription="aboutdescription1"
                android:gravity="center_horizontal"
                android:textColor="@android:color/black"/>
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/linear6"
                android:layout_marginTop="2dp"
                android:text="@string/dhiman_sir"
                tools:ignore="HardcodedText"
                android:textSize="16sp"
                android:contentDescription="aboutdescription1"
                android:gravity="center_horizontal"
                android:textColor="@android:color/black"/>
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/linear7"
                android:layout_marginTop="2dp"
                android:text="@string/Nishu_nath"
                tools:ignore="HardcodedText"
                android:textSize="16sp"
                android:contentDescription="aboutdescription1"
                android:gravity="center_horizontal"
                android:textColor="@android:color/black"/>
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/linear8"
                android:layout_marginTop="2dp"
                android:text="@string/Sojib_sir"
                tools:ignore="HardcodedText"
                android:textSize="16sp"
                android:contentDescription="aboutdescription1"
                android:gravity="center_horizontal"
                android:textColor="@android:color/black"/>
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/linear9"
                android:layout_marginTop="20dp"
                android:text="@string/Want_share"
                tools:ignore="HardcodedText"
                android:textSize="14sp"
                android:contentDescription="aboutdescription1"
                android:textColor="@android:color/black"
                android:textStyle="bold"
                android:paddingBottom="10dp" />

        </LinearLayout>
    </ScrollView>

    <LinearLayout
        android:id="@+id/footer"
        android:layout_marginTop="5dip"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:background="@android:color/white"
        android:padding="5dip">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/developer"
            android:text="@string/developer"
            android:textColor="#0b066d"
            android:textSize="15sp"
            android:textStyle="bold"
            android:layout_marginTop="15dp"
            android:layout_marginLeft="5dp"
            tools:text="Developer Info :"
            tools:ignore="RtlHardcoded" />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/myname"
            android:text="@string/shakib_saha"
            android:textColor="#913807"
            android:textSize="15sp"
            android:layout_marginLeft="5dp"
            android:textStyle="bold"
            android:layout_marginTop="8dp"
            tools:text="Shakib Saha"
            tools:ignore="RtlHardcoded" />

        <TextView
            android:text="@string/student"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/student"
            android:layout_marginLeft="5dp"
            android:textColor="#913807"
            android:textStyle="normal"
            android:textSize="14sp"
            tools:ignore="RtlHardcoded" />

        <TextView
            android:text="@string/rmstu"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/textView6"
            android:textColor="#913807"
            android:textStyle="normal"
            android:layout_marginLeft="5dp"
            android:textSize="14sp"
            tools:ignore="RtlHardcoded" />
    </LinearLayout>
</LinearLayout></FrameLayout>

0 个答案:

没有答案