Android NavigationItemSelectedListener确实无效

时间:2016-07-17 08:43:02

标签: android navigationview

我学会了使用Android NavigationView,它显示得很好,但无法响应点击事件。

这是我的代码。我想点击任何项目时显示Toast。但它不起作用......

        nav = (NavigationView) findViewById(R.id.navigation);
    //nav.setClickable(true);
    nav.setNavigationItemSelectedListener(new      NavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(MenuItem item) {
            item.setChecked(true);
            Toast.makeText(getApplicationContext(), "caonima", Toast.LENGTH_SHORT).show();
            return false;
        }
    });



<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">

<item android:icon="@drawable/homepage"
      android:title="首页"
    />
 <item android:id="@+id/psychology_item" android:title="日常心理学" android:icon="@drawable/right"/>
 <item android:id="@+id/recommand" android:title="用户推荐日报" android:icon="@drawable/right"/>
 <item android:id="@+id/movies" android:title="电影日报" android:icon="@drawable/right"/>
 <item android:id="@+id/internet" android:title="互联网安全" android:icon="@drawable/right"/>
 <item android:id="@+id/noboring" android:title="不许无聊" android:icon="@drawable/right"/>
 <item android:id="@+id/desingn" android:title="设计日报" android:icon="@drawable/right"/>

</group>

<?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"
android:fitsSystemWindows="true"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
>

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


<android.support.design.widget.NavigationView
    android:fitsSystemWindows="true"
    android:id="@+id/navigation"
    android:layout_gravity="start"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    app:headerLayout="@layout/navigation_header"
    app:menu="@menu/nav_menu"
    />
</android.support.v4.widget.DrawerLayout>

它扰乱了我两天,我发现一些问题使用listView, 或者添加drawerView.bringToFront(); drawerView.requestLayout(); 但没有改变。

顺便说一下,我在像这样的片段中使用了工具栏

 public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View v = inflater.inflate(R.layout.fragment_main_title,container,false);
    mToolbar = (Toolbar)v.findViewById(R.id.main_title_toolbar);
    mDrawerLayout = (DrawerLayout) getActivity().findViewById(R.id.drawer_layout);

    mToolbar.setTitle("首页");
    mToolbar.setTitleTextColor(getResources().getColor(R.color.text));
    ((AppCompatActivity)getActivity()).setSupportActionBar(mToolbar);

    mToolbar.setNavigationIcon(R.drawable.navigation);
    setHasOptionsMenu(true);
    mToggle =  new ActionBarDrawerToggle(getActivity(),mDrawerLayout,mToolbar,
            R.string.drawer_open,R.string.drawer_shut){
        @Override
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
            drawerView.bringToFront();
            drawerView.requestLayout();
        }

        @Override
        public void onDrawerClosed(View drawerView) {
            super.onDrawerClosed(drawerView);
        }
    };
    //mDrawerLayout.addDrawerListener(mToggle);
    mToggle.syncState();

    return v;
}

1 个答案:

答案 0 :(得分:0)

哦,我只是这么一篇文章。它解决了我的问题。 这是链接:enter link description here

你只需要把你的抽屉拉到前面, 所以尝试使用这两种方法

public class Principal {
    public static void main(String[] args) {
        String[] busNames = new String[] {"Bus1", "Bus2", "Bus3"};
        int[][] boxes = {{1, 2, 2},{1, 2, 1}, {1, 2, 2}, {1, 2, 1}};

        displaySummary(3, busNames, boxes);
    }

    public static void displaySummary(int totalShipment, String[] nameOfBus, int[][] storageBox) {
        int counter;
        //Display the Summary Report              
        System.out.println("\nSummary\n- - - - - - - - - - - -");
        for(counter = 0; counter < totalShipment; counter++)
        {
            System.out.println("\n Shipment #" + (counter + 1)+  " - " + nameOfBus[counter]);
            System.out.print(" XL = " + storageBox[0][counter] + ", ");
            System.out.print("L = " + storageBox[1][counter] + ", ");
            System.out.print("M = " + storageBox[2][counter] + ", ");
            System.out.print("S = " + storageBox[3][counter]+"\n");
        }
    }
}

然后你可以找到它解决了,如果你想了解更多,搜索&#34; Z顺序&#34;。