首页 文章

单击导航抽屉中的菜单项时,不会显示片段

提问于
浏览
0

我有一个导航抽屉,其中有很多菜单项 . 我已经制作了一个片段“Home”,然后单击Home Fragment应该打开的Home菜单项 . 但它没有打开 .

我的java代码是:

package com.example.hsports.weddingplanner.Activities;

import android.support.design.widget.NavigationView;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;


import android.os.Handler;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;

import com.example.hsports.weddingplanner.Fragments.Home;
import com.example.hsports.weddingplanner.R;



public class FrontPage extends AppCompatActivity {
    NavigationView navigationView;
    DrawerLayout drawer;
    Toolbar toolbar;
    String nameOnTitleBar[];
    int indexSelected=0;




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

        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        drawer=(DrawerLayout)findViewById(R.id.drawer_layout);
        navigationView=(NavigationView)findViewById(R.id.nav_view);

        nameOnTitleBar=getResources().getStringArray(R.array.nav_list_items);

        setupNavigationView();



    }

    private void setupNavigationView() {


        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(MenuItem item) {

                switch(item.getItemId())
                {
                    case R.id.Home:
                        indexSelected=0;
                        markTheItemSelected(indexSelected);
                        changeNameofTitleBar(indexSelected);
                        fragmentTodisplay(indexSelected);
                        break;

                    case R.id.SignIn:
                        indexSelected=1;
                        markTheItemSelected(indexSelected);
                        changeNameofTitleBar(indexSelected);
                        fragmentTodisplay(indexSelected);
                        break;

                    case R.id.SignOut:
                        indexSelected=2;
                        markTheItemSelected(indexSelected);
                        changeNameofTitleBar(indexSelected);
                        fragmentTodisplay(indexSelected);
                        break;

                    case R.id.AboutUs:
                        indexSelected=3;
                        markTheItemSelected(indexSelected);
                        changeNameofTitleBar(indexSelected);
                        fragmentTodisplay(indexSelected);
                        break;

                    case R.id.ContactUs:
                        indexSelected=4;
                        markTheItemSelected(indexSelected);
                        changeNameofTitleBar(indexSelected);
                        fragmentTodisplay(indexSelected);
                        break;

                }

                drawer.closeDrawers();
                return true;
            }
        });


        ActionBarDrawerToggle actionBarDrawerToggle=new ActionBarDrawerToggle(this,drawer,toolbar,R.string.openDrawer,R.string.closeDrawer){

            @Override
            public void onDrawerClosed(View drawerView) {
                super.onDrawerClosed(drawerView);
            }

            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
            }
        };

        actionBarDrawerToggle.syncState();
    }

    private void fragmentTodisplay(int indexSelected) {

        switch (indexSelected)
        {
            case 0:
                Home obj=new Home();
                FragmentTransaction transaction=getSupportFragmentManager().beginTransaction();
                transaction.add(R.id.frame,obj,"HOME");
                transaction.commit();
                break;
        }

    }

    private void markTheItemSelected(int indexSelected) {

        navigationView.setCheckedItem(indexSelected);
    }

    private void changeNameofTitleBar(int indexSelected) {

        toolbar.setTitle(nameOnTitleBar[indexSelected]);
    }

}

片段代码如下:Home.java

package com.example.hsports.weddingplanner.Fragments;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.hsports.weddingplanner.R;

/**
 * Created by I324671 on 11/27/2016.
 */
public class Home extends Fragment {

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        return inflater.inflate(R.layout.fragmentaboutus,container,false);

    }
}

我的navoigation_drawer.xml是这样的:

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

<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <group android:checkableBehavior="single"
        >


        <item
            android:id="@+id/Home"
            android:title="HOME"
            />
        <item
            android:id="@+id/SignIn"
            android:title="SIGN-IN"
            />

        <item
            android:id="@+id/SignOut"
            android:title="SIGN-OUT"
            />

        </group>

<item android:title="OTHER">

    <menu>

        <item
            android:id="@+id/AboutUs"
            android:title="ABOUT US"/>

        <item
            android:id="@+id/ContactUs"
            android:title="CONTACT US"
            />

        <item
            android:id="@+id/Share"
            android:title="SHARE"
            />
    </menu>

</item>
</menu>

fragmentaboutus.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">


    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="Home"


        />

</LinearLayout>

现在,当我单击导航抽屉中的主页图标时,不会显示特定的主页片段 .

这是app_bar_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent"

    android:layout_width="match_parent"
    android:fitsSystemWindows="true"

    >
    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay"
        >
        <android.support.v7.widget.Toolbar
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:id="@+id/toolbar"
            app:popupTheme="@style/AppTheme.PopupOverlay"
            android:background="@color/blue"

            >



        </android.support.v7.widget.Toolbar>
    </android.support.design.widget.AppBarLayout>


        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/frame"

            ></FrameLayout>

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

Activity_front_page.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_height="match_parent"
    android:layout_width="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:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:menu="@menu/navigation_drawer"
        android:id="@+id/nav_view"

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



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

1 回答

  • 1

    尝试更换

    transaction.add(R.id.frame, obj, "HOME");
    

    transaction.replace(R.id.frame, obj, "HOME");
    

相关问题