Android 导航抽屉主要活动内容
嗨,我在我的android应用程序的主活动java类中有导航抽屉,这是代码Android 导航抽屉主要活动内容,android,android-studio,android-fragments,navigation-drawer,Android,Android Studio,Android Fragments,Navigation Drawer,嗨,我在我的android应用程序的主活动java类中有导航抽屉,这是代码 public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener { @Override protected void onCreate(Bundle savedInstanceState) {
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
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);
//add this line to display menu1 when the activity is loaded
displaySelectedScreen(R.id.nav_menu1);
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
private void displaySelectedScreen(int itemId) {
//creating fragment object
Fragment fragment = null;
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
//initializing the fragment object which is selected
switch (itemId) {
case R.id.nav_menu1:
fragment = new gallary();
break;
case R.id.nav_menu2:
fragment = new about();
break;
case R.id.nav_menu3:
fragment = new contact();
break;
}
//replacing the fragment
if (fragment != null) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.content_frame, fragment);
ft.commit();
}
drawer.closeDrawer(GravityCompat.START);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
//calling the method displayselectedscreen and passing the id of selected menu
displaySelectedScreen(item.getItemId());
//make this method blank
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">
<include
layout="@layout/app_bar_main"
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_main"
app:menu="@menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
所以我想在应用程序的主活动xml页面中添加一些内容,比如图像和文本,但当我在导航的主内容中添加这些内容时,看起来很好,一切正常,但当我点击导航抽屉中的gallary fragment时,我进入了新页面,该页面包含我放入gallary xml中的内容,但问题是主活动的内容也出现在我从导航抽屉中单击的所有片段页面中
如果没有主要活动的内容,我如何使每个片段的内容出现
有人明白我的意思吗@_@ 我建议您不要使用除navigationView和将在屏幕上显示navigationDrawer项目的片段以外的任何元素,因为如果您单击某个项目,您将被重定向到该页面,但稍后如果您想要返回到该主活动页面,则几乎不可能。您可以重新启动该活动,或者尝试一些复杂的方法来实现这一点 相反,您应该使用片段。您可以在onResume()方法中调用打开应用程序时加载的默认片段。此片段也应在导航项中提供,以供以后参考。您可以这样做:
fragment = new YourFragment();
transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.contentfragment,fragment).commitNow();
但是,如果您仍然想这样做,可以将mainactivity的元素设置为visibility为gone。我不知道这种方法是否有效
这应该在导航项单击时设置
[your_element].setVisibility(View.GONE);
你们的碎片相互重叠。 从抽屉中选择导航项目时,请确保在添加新片段之前删除了以前的片段:
FragmentManager fm = getSupportFragmentManager();
fm.bginTransaction()
.remove(oldFragment)
.add(newFragment)
.commit();
请添加布局文件我编辑问题并添加它