导航抽屉切换活动,而不是片段

是否有可能在Android中使用导航抽屉,而不是更新片段,我想切换活动作为我的应用程序内的导航手段。

是的,这是可能的 – 这是我为我的应用程序做的。 我已经做了一些活动,而不是把它们全部转换成碎片,我想要定制导航抽屉在所有这些活动中工作。 不幸的是,这不是一个快速的解决方法,所以如果你有使用片段的select,我会去。 但不pipe我是怎么做到的

假设我有两个活动,两个都是我想要的导航抽屉。 在每个的layout.xml中,我用相应的ListView指定了一个DrawerLayout来保存我的导航选项。 从本质上说,导航抽屉是每次在活动之间切换时产生的,performance为持续。 为了让生活变得更容易,我使用了设置导航抽屉所需的常用方法,并将它们放在自己的类中: NavigationDrawerSetup.java 。 这样我的活动可以使用相同的自定义适配器等。

在这个NavigationDrawerSetup.java类中,我有以下几点:

  • configureDrawer() – 这设置了ActionBarActionBarDrawerToggle和所需的侦听器
  • 我的自定义数组适配器(填充列表中的导航选项)
  • selectOptions()方法,处理抽屉项目点击

当您在其中一个活动中设置导航抽屉时,只需创build一个新的NavigationDrawerSetup对象并传入所需的布局参数(如DrawerLayoutListView等)即可。 然后你可以调用configureDrawer()

  navigationDrawer = new NavigationDrawerSetup(mDrawerView, mDrawerLayout, mDrawerList, actionBar, mNavOptions, currentActivity); navigationDrawer.configureDrawer(); 

由于导航抽屉与您所在的活动绑定, currentActivity被传入。 设置ActionBarDrawerToggle时,您将不得不使用它:

 mDrawerToggle = new ActionBarDrawerToggle(currentActivity, // host Activity mDrawerLayout, /* DrawerLayout object */ R.drawable.ic_drawer, /* nav drawer image to replace 'Up' caret */ R.string.drawer_open, /* "open drawer" description for accessibility */ R.string.drawer_close /* "close drawer" description for accessibility */ ) 

设置自定义Adapter时,您还需要使用currentActivity

至于如何通过导航抽屉来切换活动,你可以在你的selectItem()方法中设置新的意图:

 private void selectItem(int position) { // Handle Navigation Options Intent intent; switch (position) { case 0: intent = new Intent(currentActivity, NewActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); currentActivity.startActivity(intent); break; case 1: // etc. } 

只要确保你的新Activity也有导航抽屉设置,它应该显示。

有很多事情可以根据自己的需要来定制这种方法,但这是我如何做到的一般结构。 希望这可以帮助!

您需要实现导航抽屉的BaseDrawerActivity ,然后在需要导航抽屉的每个活动中扩展BaseDrawerActivity

首先创buildBaseDrawerActivity.java

 public class BaseDrawerActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener{ DrawerLayout drawerLayout; Toolbar toolbar; FrameLayout frameLayout; NavigationView navigationView; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); super.setContentView(R.layout.activity_base_drawer);; Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); frameLayout = (FrameLayout) findViewById(R.id.content_frame); drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); ActionBarDrawerToggle toggle = new ActionBarDrawerToggle( this, drawerLayout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); drawerLayout.setDrawerListener(toggle); toggle.syncState(); navigationView = (NavigationView) findViewById(R.id.nav_view); navigationView.setNavigationItemSelectedListener(this); } @Override public void onBackPressed() { if (drawerLayout.isDrawerOpen(GravityCompat.START)) { drawerLayout.closeDrawer(GravityCompat.START); } else { super.onBackPressed(); } } @Override public boolean onNavigationItemSelected(MenuItem item) { int id = item.getItemId(); //to prevent current item select over and over if (item.isChecked()){ drawerLayout.closeDrawer(GravityCompat.START); return false; } if (id == R.id.nav_camera) { // Handle the camera action startActivity(new Intent(getApplicationContext(), CameraActivity.class)); } else if (id == R.id.nav_gallery) { startActivity(new Intent(getApplicationContext(), GalleryActivity.class)); } else if (id == R.id.nav_slideshow) { startActivity(new Intent(getApplicationContext(), SlideshowActivity.class)); } else if (id == R.id.nav_manage) { startActivity(new Intent(getApplicationContext(), ManageActivity.class)); } else if (id == R.id.nav_share) { startActivity(new Intent(getApplicationContext(), ShareActivity.class)); } else if (id == R.id.nav_send) { startActivity(new Intent(getApplicationContext(), SendActivity.class)); } drawerLayout.closeDrawer(GravityCompat.START); return true; } } 

然后在res/layout文件夹中创buildactivity_base_drawer.xml

 <?xml version="1.0" encoding="utf-8"?> <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" tools:openDrawer="start"> <include layout="@layout/app_bar_home"/> <android.support.design.widget.NavigationView android:id="@+id/nav_view" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="start" app:headerLayout="@layout/nav_header_home" app:menu="@menu/activity_home_drawer" /> </android.support.v4.widget.DrawerLayout> 

其中@layout/app_bar_home是:

 <?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"> <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:popupTheme="@style/AppTheme.PopupOverlay" /> </android.support.design.widget.AppBarLayout> <FrameLayout android:id="@+id/content_frame" android:layout_width="match_parent" android:layout_height="match_parent" /> </android.support.design.widget.CoordinatorLayout> 

接下来你input你的活动,将有导航抽屉如CameraActivity.java

 public class CameraActivity extends BaseDrawerActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getLayoutInflater().inflate(R.layout.activity_camera, frameLayout); /** * Setting title */ setTitle("Camera"); } @Override protected void onResume() { super.onResume(); // to check current activity in the navigation drawer navigationView.getMenu().getItem(0).setChecked(true); } } 

其中R.layout.activity_cameraCameraActivity.java的布局。

然后创build其他Activity,如GalleryActivity.java等将有导航抽屉:

 public class GalleryActivity extends BaseDrawerActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getLayoutInflater().inflate(R.layout.activity_gallery, frameLayout); // Setting title setTitle("Gallery"); } @Override protected void onResume() { super.onResume(); navigationView.getMenu().getItem(1).setChecked(true); } } 

作为@ David-Crozier指出的解决scheme的一点改进,为了避免两个animation(closuresNavigationDrawer和开始一个新的活动)的重叠,你可以在你的方法中包含一些延迟,就像在iosched应​​用程序中做的那样v2014:

 private void onNavDrawerItemClicked(final int itemId) { if (itemId == getSelfNavDrawerItem()) { mDrawerLayout.closeDrawer(GravityCompat.START); return; } if (isSpecialItem(itemId)) { goToNavDrawerItem(itemId); } else { // launch the target Activity after a short delay, to allow the close animation to play mHandler.postDelayed(new Runnable() { @Override public void run() { goToNavDrawerItem(itemId); } }, NAVDRAWER_LAUNCH_DELAY); // change the active item on the list so the user can see the item changed setSelectedNavDrawerItem(itemId); // fade out the main content View mainContent = findViewById(R.id.main_content); if (mainContent != null) { mainContent.animate().alpha(0).setDuration(MAIN_CONTENT_FADEOUT_DURATION); } } mDrawerLayout.closeDrawer(GravityCompat.START); } 

以下链接供参考: https : //github.com/google/iosched/blob/master/android/src/main/java/com/google/samples/apps/iosched/ui/BaseActivity.java