如何更改 NavigationDrawer 中的显示内容?
Posted
技术标签:
【中文标题】如何更改 NavigationDrawer 中的显示内容?【英文标题】:How to change the display content in a NavigationDrawer? 【发布时间】:2017-03-13 23:13:42 【问题描述】:在 NavigationDrawer 中选择项目后如何更改要显示的内容?
我的意思是,例如,我在 NavigationDrawer 中选择了 LogIn 选项,现在我想将显示的内容更改为 XML 文件中的另一个。
MainActivity (LauncherActivity) Java 文件:
package com.dotapps.sosgram;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import net.hockeyapp.android.UpdateManager;
import net.hockeyapp.android.CrashManager;
import net.hockeyapp.android.LoginManager;
public class LauncherActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.navbar);
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);
LoginManager.register(this, "SECRET", LoginManager.LOGIN_MODE_EMAIL_PASSWORD);
LoginManager.verifyLogin(this, getIntent());
checkForUpdates();
@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.launcher, 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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings)
return true;
return super.onOptionsItemSelected(item);
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item)
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_login)
// Here I want to change the layout
else if (id == R.id.nav_signup)
else if (id == R.id.nav_fastsos)
else if (id == R.id.nav_about)
else if (id == R.id.nav_about)
else if (id == R.id.nav_settings)
else if (id == R.id.nav_share)
else if (id == R.id.nav_exit)
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
// HockeyApp
@Override
public void onResume()
super.onResume();
checkForCrashes();
@Override
public void onPause()
super.onPause();
unregisterManagers();
@Override
public void onDestroy()
super.onDestroy();
unregisterManagers();
private void checkForCrashes()
CrashManager.register(this);
private void checkForUpdates()
UpdateManager.register(this);
private void unregisterManagers()
UpdateManager.unregister();
内容: Google Drive - (由于我的代表很少,我必须通过云端硬盘分享)
有人知道怎么做吗?
PD:我已尝试使用 setContentView(R.layout.login)
,但应用程序崩溃
【问题讨论】:
使用片段或活动。创建一个菜单抽屉和一个开关,以便单击时您将转到相应的片段/活动。 谢谢@ScottS。我已经使用 FragmentManager 解决了 【参考方案1】:你可以用片段来做到这一点。有关更多信息,请查看此链接:
http://chrisrisner.com/Using-Fragments-with-the-Navigation-Drawer-Activity
https://www.simplifiedcoding.net/android-navigation-drawer-example-using-fragments/
【讨论】:
以上是关于如何更改 NavigationDrawer 中的显示内容?的主要内容,如果未能解决你的问题,请参考以下文章
如何同时使navigationdrawer选框的listview中的所有textView?