我做了一个导航抽屉,就像developer.android.com网站上的教程中显示的那样。但是现在,我想使用一个导航抽屉,我在NavigationDrawer.class中创建了多个活动在我的应用程序。

我的问题是,这里是否有人可以做一个小教程,它解释了如何使用一个导航抽屉的多个活动。

我第一次读到这个答案 Android导航抽屉在多个活动

但在我的项目上行不通

public class NavigationDrawer extends Activity {
public DrawerLayout drawerLayout;
public ListView drawerList;
private ActionBarDrawerToggle drawerToggle;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawerToggle = new ActionBarDrawerToggle((Activity) this, drawerLayout, R.drawable.ic_drawer, 0, 0) {

        public void onDrawerClosed(View view) {
            getActionBar().setTitle(R.string.app_name);
        }

        public void onDrawerOpened(View drawerView) {
            getActionBar().setTitle(R.string.menu);
        }
    };
    drawerLayout.setDrawerListener(drawerToggle);

    getActionBar().setDisplayHomeAsUpEnabled(true);
    getActionBar().setHomeButtonEnabled(true);

    listItems = getResources().getStringArray(R.array.layers_array);
    drawerList = (ListView) findViewById(R.id.left_drawer);
    drawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_item, android.R.id.text,
            listItems));
    
    drawerList.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3) {
            drawerClickEvent(pos);
        }
    });
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    if (drawerToggle.onOptionsItemSelected(item)) {
        return true;
    }
    return super.onOptionsItemSelected(item);

}

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    drawerToggle.syncState();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    drawerToggle.onConfigurationChanged(newConfig);
}
}

在这个活动中,我想有导航抽屉,所以我扩展了'NavigationDrawer',在其他一些活动中,我想使用相同的导航抽屉

  public class SampleActivity extends NavigationDrawer {...}

当前回答

在baseactivity中更新此代码。不要忘记在你的activity xml中包含drawer_list_header。

super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
setContentView(R.layout.drawer_list_header);

并且不要在你的活动中使用request()。但仍然是抽屉不可见点击图像..和拖动它将可见没有列表项。我尝试了很多,但没有成功。需要一些锻炼…

其他回答

如果你想要一个导航抽屉,你应该使用片段。 上周我学习了这个教程,效果很好:

http://developer.android.com/training/implementing-navigation/nav-drawer.html

您还可以从本教程下载示例代码,以了解如何做到这一点。


没有片段:

这是你的BaseActivity代码:

public class BaseActivity extends Activity
{
    public DrawerLayout drawerLayout;
    public ListView drawerList;
    public String[] layers;
    private ActionBarDrawerToggle drawerToggle;
    private Map map;
    
    protected void onCreate(Bundle savedInstanceState)
    {
        // R.id.drawer_layout should be in every activity with exactly the same id.
        drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        
        drawerToggle = new ActionBarDrawerToggle((Activity) this, drawerLayout, R.drawable.ic_drawer, 0, 0) 
        {
            public void onDrawerClosed(View view) 
            {
                getActionBar().setTitle(R.string.app_name);
            }
    
            public void onDrawerOpened(View drawerView) 
            {
                getActionBar().setTitle(R.string.menu);
            }
        };
        drawerLayout.setDrawerListener(drawerToggle);
    
        getActionBar().setDisplayHomeAsUpEnabled(true);
        getActionBar().setHomeButtonEnabled(true);
        
        layers = getResources().getStringArray(R.array.layers_array);
        drawerList = (ListView) findViewById(R.id.left_drawer);
        View header = getLayoutInflater().inflate(R.layout.drawer_list_header, null);
        drawerList.addHeaderView(header, null, false);
        drawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_item, android.R.id.text1,
                layers));
        View footerView = ((LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(
                R.layout.drawer_list_footer, null, false);
        drawerList.addFooterView(footerView);
    
        drawerList.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3) {
                map.drawerClickEvent(pos);
            }
        });
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    
        if (drawerToggle.onOptionsItemSelected(item)) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    
    }
    
    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        drawerToggle.syncState();
    }
    
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        drawerToggle.onConfigurationChanged(newConfig);
    }
}

所有其他需要有导航抽屉的活动都应该扩展这个活动,而不是活动本身,例如:

public class AnyActivity extends BaseActivity
{
    //Because this activity extends BaseActivity it automatically has the navigation drawer
    //You can just write your normal Activity code and you don't need to add anything for the navigation drawer
}

XML

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <!-- The main content view -->
    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
        <!-- Put what you want as your normal screen in here, you can also choose for a linear layout or any other layout, whatever you prefer -->
    </FrameLayout>
    <!-- The navigation drawer -->
    <ListView android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
        android:background="#111"/>
</android.support.v4.widget.DrawerLayout>

编辑:

我自己也遇到过一些困难,所以如果你得到nullpointerexception,这里有一个解决方案。在BaseActivity中将onCreate函数更改为受保护的void onCreateDrawer()。其余的可以保持不变。在扩展BaseActivity的Activities中,将代码按以下顺序放置:

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity);
    super.onCreateDrawer();

这就是如何创建具有多个活动的导航抽屉,如果您有任何问题,请随时询问。


编辑2:

正如@GregDan所说,你的BaseActivity也可以覆盖setContentView()并调用onCreateDrawer:

@Override 
public void setContentView(@LayoutRes int layoutResID) 
{ 
    super.setContentView(layoutResID); 
    onCreateDrawer() ;
}

我的建议是:完全不要使用活动,而是使用片段,并将它们替换到容器中(例如线性布局),在其中显示您的第一个片段。[注意:你可以在导航图中使用这个概念。Compose进一步减少了制作xml布局的需要,所以我们也可以将其应用于此。]

代码可以在Android开发者教程中找到,你只需要自定义。

http://developer.android.com/training/implementing-navigation/nav-drawer.html

这是明智的,你应该在你的应用程序中使用越来越多的片段,应该只有四个基本的活动,你在你的AndroidManifest.xml中提到除了外部的(例如FacebookActivity):

SplashActivity: uses no fragment, and uses FullScreen theme. LoginSignUpActivity: Do not require NavigationDrawer at all, and no back button as well, so simply use the normal toolbar, but at the least, 3 or 4 fragments will be required. Uses no-action-bar theme HomeActivity or DashBoard Activity: Uses no-action-bar theme. Here you require Navigation drawer, also all the screens that follow will be fragments or nested fragments, till the leaf view, with the shared drawer. All the settings, user profile and etc. will be here as fragments, in this activity. The fragments here will not be added to the back stack and will be opened from the drawer menu items. In the case of fragments that require back button instead of the drawer, there is a fourth kind of activity below. Activity without drawer. This activity has a back button on top and the fragments inside will be sharing the same action-bar. These fragments will be added to the back-stack, as there will be a navigation history.

[进一步指导请参见:https://stackoverflow.com/a/51100507/787399]

快乐编码!!

在MainActivity中使用fragment创建导航抽屉。 初始化MainActivity中的导航抽屉 现在,在所有其他活动中,你想使用相同的导航抽屉,把DrawerLayout作为基础,把fragment作为导航抽屉。只需设置android:name在你的片段指向你的片段Java文件。您不需要在其他活动中初始化片段。 你可以通过在谷歌播放商店应用程序中滑动其他活动来访问导航抽屉

这将在接下来的视频教程中详细阐述

使用基本活动的多个活动上的导航抽屉

创建一个基本导航抽屉活动,并将这个基本导航抽屉活动扩展到你想要显示导航抽屉的所有活动是非常容易的,

制作导航菜单,标题 为导航抽屉创建一个基本活动 创建内容布局 组合菜单,标题,内容布局的基础活动 通过使用框架布局,在抽屉菜单中插入每个活动。

所有步骤在视频中都有清楚的解释

我已经找到了最好的实现。在谷歌I/O 2014应用程序中。

他们使用和凯文一样的方法。如果你可以把自己从I/O应用程序中所有不需要的东西中抽象出来,你可以提取你需要的一切,谷歌保证它是导航抽屉模式的正确使用。 每个活动都有一个可选的DrawerLayout作为其主布局。有趣的部分是如何导航到其他屏幕。它在BaseActivity中是这样实现的:

private void goToNavDrawerItem(int item) {
        Intent intent;
        switch (item) {
            case NAVDRAWER_ITEM_MY_SCHEDULE:
                intent = new Intent(this, MyScheduleActivity.class);
                startActivity(intent);
                finish();
                break;

这与用片段事务替换当前片段的常见方法不同。但用户并没有发现视觉上的差异。