I learnded to use Android NavigationView,it was shown well but could't response to click event.
Here are my codes. I want to show a Toast when i click any item.but it doesn't work...
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>
it has disturbed me for two days, i find some question use listView , or add drawerView.bringToFront();drawerView.requestLayout(); But nothing changed.
by the way , i used toolbar in a fragment like this
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;
}