1
我有一個NavigationDrawer應用,但是,onNavigationItemSelected方法未被調用。抽屜正常工作,當一個項目被選中的抽屜確實接近,但意見不加載/改變onNavigationItemSelected未在導航抽屜中調用
我的主類
public class BRGO extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_brgo);
News initial = new News();
FragmentTransaction transfer = getSupportFragmentManager().beginTransaction();
transfer.replace(R.id.fragmentcontainer, initial);
transfer.commit();
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.brgo, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
//noinspection SimplifiableIfStatement
if (id == R.id.S14273) {
editor.putInt("School", 14273);
editor.commit();
return true;
}
else if(id == R.id.S14276){
editor.putInt("School", 14276);
editor.commit();
return true;
}
else if(id == R.id.S14274){
editor.putInt("School", 14274);
editor.commit();
return true;
}
else if(id == R.id.S14271){
editor.putInt("School", 14271);
editor.commit();
return true;
}
else if(id == R.id.S14278){
editor.putInt("School", 14278);
editor.commit();
return true;
}
else if(id == R.id.S14277){
editor.putInt("School", 14273);
editor.commit();
return true;
}
else if(id == R.id.S14275){
editor.putInt("School", 14275);
editor.commit();
return true;
}
else if(id == R.id.S14272){
editor.putInt("School", 14272);
editor.commit();
return true;
}
else if(id == R.id.S14269){
editor.putInt("School", 14269);
editor.commit();
return true;
}
else if(id == R.id.S14268){
editor.putInt("School", 14268);
editor.commit();
return true;
}
else if(id == R.id.S14264){
editor.putInt("School", 14264);
editor.commit();
return true;
}
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
Fragment fragment;
switch (id) {
case R.id.nav_News:
fragment = News.newInstance();
break;
case R.id.nav_Calendar:
fragment = Calendar.newInstance();
break;
case R.id.nav_Websites:
fragment = Websites.newInstance();
break;
case R.id.nav_About:
fragment = About.newInstance();
default:
fragment = News.newInstance();
break;
}
FragmentTransaction transfer = getSupportFragmentManager().beginTransaction();
transfer.replace(R.id.fragmentcontainer,fragment);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
我的主要XML
<android.support.v4.widget.DrawerLayout 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:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start"
android:background="@color/colorPrimary">
<include
layout="@layout/app_bar_brgo"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<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/nav_header_brgo"
app:menu="@menu/activity_brgo_drawer"
app:itemTextColor="@color/colorPrimaryDark"
app:itemBackground="@color/colorPrimary"
android:background="@color/colorPrimary"/>
</android.support.v4.widget.DrawerLayout>
我的應用程序欄XML
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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:fitsSystemWindows="true"
tools:context="com.tble.brgo.BRGO">
<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:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:titleTextColor="@color/colorPrimaryDark"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:id="@+id/fragmentcontainer"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</android.support.design.widget.CoordinatorLayout>
這仍然不會解決我目前的問題。主要問題是,當我點擊NavigationDrawer項目時,從不會調用覆蓋方法。因此交易永遠不會發生。 – Praveen