是的,这是可能的——这就是我为我的应用所做的。我已经设置了许多活动,而不是将它们全部转换为片段,我想要定制导航抽屉以在它们所有的活动中使用。不幸的是,这不是一个快速的变通方法,所以如果您有使用片段的选项,我建议您使用片段。但无论如何,这就是我做的方式:
假设我有2个活动,我都希望有导航抽屉。在每个 layout.xml 中,我指定了一个 DrawerLayout
, 以及相应的 ListView
来保存我的导航选项。实质上,每次我在活动之间切换时,都会创建导航抽屉,从而给人们留下持久的印象。为了让生活变得更加轻松,我将设置导航抽屉所需的公共方法放入了自己的类中:NavigationDrawerSetup.java
。这样,我的活动可以使用相同的自定义适配器等。
在这个 NavigationDrawerSetup.java
类内部,我有以下内容:
configureDrawer()
- 这设置了 ActionBar
、ActionBarDrawerToggle
和所需的侦听器selectOptions()
方法,用于处理抽屉项目的点击当您在其中一个活动中设置导航抽屉时,只需创建一个新的 NavigationDrawerSetup
对象并传入所需的布局参数(如 DrawerLayout
、ListView
等),然后调用 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 */
)
你在设置自定义适配器时,还需要使用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
。
首先创建BaseDrawerActivity.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
文件夹中创建activity_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>
接下来,您需要输入具有导航抽屉的活动,例如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_camera
是您的CameraActivity.java
的布局。
然后创建其他活动,如具有导航抽屉的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);
}
}
setContentView()
可以被重写。https://dev59.com/0n7aa4cB1Zd3GeqPkwVg#22652821 - h8pathak作为对@David-Crozier提出的解决方案的一点改进,在关闭NavigationDrawer并启动新活动的两个动画重叠时,您可以在您的方法中包含一点延迟,就像在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://guides.codepath.com/android/fragment-navigation-drawer#alternative-to-fragments
你可以通过填充布局来代替使用片段管理器。 <activity
android:id="@+id/nav_custom"
android:name="net.larntech.nav.CustomActivity"
android:label="@string/menu_custom"
tools:layout="@layout/activity_custom"
/>
您可以通过下面的链接找到完整的教程。 Android Studio导航抽屉