如何使用 Fragments 打开来自 menu.xml 的链接

How to open links from menu.xml using Fragments

我正在尝试从 menu.xml 添加可点击的项目。它只是不工作,甚至 Toast 效果。 布局如下copy/paste,您可以查看。 当我尝试使用 id 时,有我的 MainActivity.java

主要活动:

    public class MainActivity extends AppCompatActivity {

    private WebView mWebView;

    private SectionsPagerAdapter mSectionsPagerAdapter;
    private static final String TAG = "MainActivity";
    private ViewPager mViewPager;
    ActionBarDrawerToggle actionBarDrawerToggle;

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

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        //setSupportActionBar(toolbar);
        toolbar.setNavigationIcon(R.drawable.hamburgermenu);

        DrawerLayout drawerLayout = findViewById(R.id.drawer_layout);

        actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close);

        drawerLayout.addDrawerListener(actionBarDrawerToggle);

        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

        mViewPager = (ViewPager) findViewById(R.id.container);
        mViewPager.setAdapter(mSectionsPagerAdapter);

        TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);

        mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
        tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager));

        tabLayout.getTabAt(0).setText("Home").setIcon(R.drawable.homeicon);
        tabLayout.getTabAt(1).setText("Section 1").setIcon(R.drawable.explorer);
        tabLayout.getTabAt(2).setText("Section 2").setIcon(R.drawable.innovator);
        tabLayout.getTabAt(3).setText("Section 3").setIcon(R.drawable.collar);
        tabLayout.setTabTextColors(getResources().getColorStateList(R.color.white));

    }

    @Override
    public void onBackPressed() {
        if(mWebView.canGoBack()) {
            mWebView.goBack();
        } else {
            super.onBackPressed();
        }
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        actionBarDrawerToggle.syncState();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);

        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch(item.getItemId()){
            case R.id.dashboard:
                Intent intentExplorer = new Intent(this, Tab1Explorer.class);
                startActivity(intentExplorer);
                break;
            case R.id.action_search:
                //Display Toast for test
                Log.d(TAG,"test");
                Toast.makeText(this,"Search!", Toast.LENGTH_SHORT).show();
                break;
            case R.id.action_help:
                Intent intentContact = new Intent(this,Tab3Collar.class);
                startActivity(intentContact);
                break;
            case R.id.action_about:
                Toast.makeText(this,"This is simple About Demo", Toast.LENGTH_LONG).show();
                break;
            default:
                Toast.makeText(this,"XYZ", Toast.LENGTH_LONG).show();
                return super.onOptionsItemSelected(item);
        }

        Toast.makeText(this,"XYZ", Toast.LENGTH_LONG).show();
        return super.onOptionsItemSelected(item);
    }

    public class SectionsPagerAdapter extends FragmentPagerAdapter {

        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            switch(position) {
                case 0:
                    Tab0Home tab0 = new Tab0Home();
                    return tab0;
                case 1:
                    Tab1Section tab1 = new Tab1Section();
                    return tab1;
                case 2:
                    Tab2Section tab2 = new Tab2Section();
                    return tab2;
                case 3:
                    Tab3Section tab3 = new Tab3Section();
                    return tab3;
                default:
                    return null;
            }
        }

        @Override
        public int getCount() {
            // Show 4 total pages.
            return 4 ;
        }

        @Nullable
        @Override
        public CharSequence getPageTitle(int position) {
            switch (position){
                case 0:
                    return "Home";
                case 1:
                    return "Section11";
                case 2:
                    return "Section22";
                case 3:
                    return "Section33";
            }
            return null;
        }
    }
}

menu_main.xml 文件看起来不错。我有 android:id 的图标、标题...

Activity_main.xml 文件看起来:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical">


    <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.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="?attr/colorPrimary"
            android:minHeight="?attr/actionBarSize"
            android:theme="?attr/actionBarTheme"
            app:title="@string/app_name"/>

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabTextAppearance="@style/CustomTextStyle">

            <android.support.design.widget.TabItem
                android:id="@+id/tabItem0"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/tab_text_0" />

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

            <android.support.design.widget.TabItem
                android:id="@+id/tabItem2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/tab_text_2" />

            <android.support.design.widget.TabItem
                android:id="@+id/tabItem3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/tab_text_3" />


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

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

    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="start">

        <android.support.design.widget.NavigationView
            android:id="@+id/navigation_view"
            android:layout_width="200dp"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:background="@color/white"
            app:menu="@menu/menu_main">

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

        <android.support.v4.view.ViewPager

            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

        </android.support.v4.view.ViewPager>

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

</LinearLayout>

当我从菜单中单击 "Search" 时,我只想显示例如 Toast,但它不起作用。它只是折叠汉堡包菜单而没有效果。
我应该改变什么?

导航视图不使用onOptionsItemSelected方法,需要调用navigationView.setOnNavigationItemSelectedListener