单击菜单项后,我的导航抽屉没有关闭?

时间:2017-04-12 08:12:48

标签: android android-fragments android-navigation-drawer

我是android的初学者。我想创建一个导航菜单抽屉。当我点击图标时它会打开。当我点击菜单项时它会显示片段。但是当我单击菜单项时,我的导航抽屉不会自动关闭。我每次关闭导航抽屉看我的片段。我不知道在哪里改变我的编码?在此我附上了我的MainActivity.java,activity_main.xml。有人请帮帮我。

activity_main.xml中

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/drawer_layout"
    tools:context="com.example.yuvi.navdrawermanual.MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textAlignment="center"
        android:orientation="vertical">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/main_msg"
            android:textSize="20dp"
            android:textAlignment="center"
            android:text="@string/main_layout"/>
        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/frame_container">
        </FrameLayout>
    </LinearLayout>

    <android.support.design.widget.NavigationView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:id="@+id/main_drawer"
        app:menu="@menu/navigation_menu"
        android:layout_gravity="start">
    </android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity {
    private DrawerLayout mDrawerLayout;
    private ActionBarDrawerToggle mToggle;
    private NavigationView navigationView;
    private android.app.FragmentTransaction fragmentTransaction;

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

        mDrawerLayout=(DrawerLayout)findViewById(R.id.drawer_layout);
        mToggle=new ActionBarDrawerToggle(this,mDrawerLayout,R.string.open,R.string.close);
        navigationView=(NavigationView) findViewById(R.id.main_drawer);

        mDrawerLayout.addDrawerListener(mToggle);
        mToggle.syncState();
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                int id=item.getItemId();
                android.app.FragmentManager fragmentManager= getFragmentManager();
                if(id==R.id.nav_acc){
                    fragmentManager.beginTransaction().replace(R.id.frame_container,new FirstFragment()).commit();
                }
                else if(id==R.id.nav_set){
                    fragmentManager.beginTransaction().replace(R.id.frame_container,new SecondFragment()).commit();
                }
                else if(id==R.id.nav_logout){
                    fragmentManager.beginTransaction().replace(R.id.frame_container,new ThirdFragment()).commit();
                }
                return false;
            }
        });
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if(mToggle.onOptionsItemSelected(item)){
            return true;
        }
        return super.onOptionsItemSelected(item);
    } 
}

2 个答案:

答案 0 :(得分:2)

把这一行:

mDrawerLayout.closeDrawer(GravityCompat.START);

后:

navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                int id=item.getItemId();
     /*-------------------HERE--------------------*/

答案 1 :(得分:0)

可以使用

drawerLayout.closeDrawer(GravityCompat.START)

drawerLayout.closeDrawers()