Java – Android navigationview cannot respond to the click event on the project
•
Android
I'm trying to add a Navigation view to my application, but for some reason, I can't make it respond to any click events on any of the items it contains
My activity_ The main.xml file is as follows:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true" >
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/navheader"
app:menu="@menu/menu_navigation" />
<LinearLayout
...
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
In my mainactivity.java, I have this code to create the navigationview and its project selection listener:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
...
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(
new NavigationView.OnNavigationItemSelectedListener() {
// This method will trigger on item Click of navigation menu
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
if(menuItem.isChecked()) menuItem.setChecked(false);
else menuItem.setChecked(true);
mDrawerLayout.closeDrawers();
switch(menuItem.getItemId()) {
case R.id.nav_item_fragment:
// open a fragment
Toast.makeText(getApplicationContext(),"Items Selected",Toast.LENGTH_SHORT).show();
break;
case R.id.nav_logs_fragment:
// open a fragment
Toast.makeText(getApplicationContext(),"Logs Selected",Toast.LENGTH_SHORT).show();
break;
case R.id.nav_settings_fragment:
// open a fragment
Toast.makeText(getApplicationContext(),"Settings Selected",Toast.LENGTH_SHORT).show();
break;
default:
return true;
}
return true;
}
});
final ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setHomeAsUpIndicator(R.drawable.ic_menu_white_24dp);
actionBar.setDisplayHomeAsUpEnabled(true);
}
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer);
toggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar,
R.string.navigation_drawer_open, R.string.navigation_drawer_close) {
@Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
}
@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
}
};
fragmentManager = getSupportFragmentManager();
...
}
This problem really puzzles me, because I used the same code in the old application I wrote, and it seems to work normally. I'm using Android SDK 22 and build tools 22.0.1
resolvent:
With the same problem, this is my solution
I add headerview to navigationview programmatically, so I already have navigationview
I called navigationview. Bringtorfront();
This is the code snippet for the context
NavigationView navigationView = (NavigationView) findViewById(R.id.navigation_view);
navigationView.bringToFront();
Here is this code snippet inch GitHub repo for my project
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码