我有一个NavigationDrawer应用程序,但是没有调用onNavigationItemSelected方法。抽屉正常工作,当选择一个项目时,抽屉关闭,但视图没有加载/更改
我的主要课程
Java Compiler
我的主要XML
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>
答案 0 :(得分:0)
如果没有提交,FragmentTransaction就不完整。
改变这个:
FragmentTransaction transfer = getSupportFragmentManager().beginTransaction();
transfer.replace(R.id.fragmentcontainer,fragment);
为:
FragmentTransaction transfer = getSupportFragmentManager().beginTransaction();
transfer.replace(R.id.fragmentcontainer,fragment).commit();