导航抽屉和Android中的活动

时间:2014-09-04 16:14:44

标签: android android-fragments navigation android-actionbar navigation-drawer

我正在试用this tutorial.

中给出的导航抽屉(幻灯片菜单)

与上面的链接和我的区别在于,我试图调用活动而不是调用片段。当应用程序打开时,我无法看到导航抽屉菜单我只能看到打开了HOME活动的操作栏。

以下是我更改的代码:(是否需要有片段或者我可以在导航抽屉中使用活动作为我的第一个屏幕?)

    mTitle = mDrawerTitle = getTitle();

    navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);
    navMenuIcons = getResources().obtainTypedArray(R.array.nav_drawer_icons);
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerList = (ListView) findViewById(R.id.left_drawer);    

    navDrawerItems = new ArrayList<NavDrawerItem>();

    navDrawerItems.add(new NavDrawerItem(navMenuTitles[0], navMenuIcons.getResourceId(1, -1)));
    navDrawerItems.add(new NavDrawerItem(navMenuTitles[1], navMenuIcons.getResourceId(2, -1),true, "200"));

    navMenuIcons.recycle();

    mDrawerList.setOnItemClickListener(new SlideMenuClickListener());

    adapter = new NavDrawerListAdapter(getApplicationContext(), navDrawerItems);
    mDrawerList.setAdapter(adapter);

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

    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
            R.drawable.drawer,
            R.string.drawer_open,
            R.string.drawer_close
            ) 
    {
        public void onDrawerClosed(View view) 
        {
            getActionBar().setTitle(mTitle);
            invalidateOptionsMenu();
        }

        public void onDrawerOpened(View drawerView) 
        {
            getActionBar().setTitle(mDrawerTitle);
            invalidateOptionsMenu();
        }
    };
    mDrawerLayout.setDrawerListener(mDrawerToggle);

    if (savedInstanceState == null) 
    {
        displayView(0);
    }
}

private class SlideMenuClickListener implements
ListView.OnItemClickListener
{
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
    {
        displayView(position);
    }
}
private void displayView(int position) 
{
    switch (position) 
    {
    case 0:
        //fragment = new HomeFragment();        

        Intent intent = new Intent(this, Home.class);
        startActivity(intent);

        return;

    case 1:
        //fragment = new FindPeopleFragment();

        Intent intent1 = new Intent(this, Profile.class);
        startActivity(intent1);
        break;

    case 2:
        //fragment = new PhotosFragment();

        Intent intent2 = new Intent(this, Details.class);
        startActivity(intent2);
        break;

    default:
        break;
    }

    mDrawerList.setItemChecked(position, true);
    mDrawerList.setSelection(position);
    setTitle(navMenuTitles[position]);
    mDrawerLayout.closeDrawer(mDrawerList);
}

如何修复此问题以在我的家庭活动中显示导航抽屉?

更新:

我甚至尝试了以下链接中给出的以下选项:

How can I call one of my activity using navigation drawer ?但我仍未获得导航幻灯片菜单。

5 个答案:

答案 0 :(得分:2)

你做不到...... 导航抽屉是活动的布局,您无法在另一个活动中显示活动,您需要使用片段!

活动是一个屏幕,你不能在另一个屏幕内显示一个屏幕,但一个片段可能是一个屏幕的组成部分,你可以在一个活动的容器内膨胀一个片段,然后向用户显示。

如果您不这样做,您可以创建一个抽象活动并继承,但您不会在片段中拥有活动,您将拥有多个活动,每个活动都有您自己的导航抽屉。

答案 1 :(得分:1)

如果您查看Android导航抽屉的示例文档,则明确定义使用片段而不是活动来从导航栏加载不同的“页面”。随着Android的应用程序生命周期的发展,片段更容易加载,对Android系统的影响也更小。它们也可以很容易地设置在背景中,并被其他不同的片段取代。

话虽如此,您最好的方法是将您的活动转换为片段,并使用它们加载您应用中所需的不同页面。这不是一项艰巨的任务,我将告诉你如何:

  1. 将所有活动更改为片段

    public class nameOfFragment extends Fragment { }
    
  2. 使用onCreateView方法而不是onCreate

    public View onCreateView() {
    
        View view = inflater.inflate(R.layout.activity_main, container, false);
    
        //any other elements in that view need to be included here in this manner.
        Button rate = (Button) rootView.findViewById(R.id.rate);
    
        return view;
    }
    
  3. 这些更改应足以将您的活动更改为片段。 您需要对通过导航栏访问的所有片段执行相同的操作。

    如果您需要任何进一步的帮助,请发布您正在使用的活动的代码。 希望这会有所帮助:)

答案 2 :(得分:1)

你有代码来构建和显示导航栏吗?将您的代码放在基本活动类中。然后,您要访问导航栏的每个活动都应该从该基类继承。

Google的IO 2014应用也会做同样的事情,看看他们的来源here

执行此操作时,您需要找到一种方法来在转换到下一个活动时保持抽屉的状态。我不知道该怎么做,但你可能会在IO应用程序的源代码中找到答案。

答案 3 :(得分:1)

如果您必须有单个抽屉实例,那么您必须通过片段。

如果除了活动之外别无选择,请为所有活动定义基类(比如BaseActivity)。在BaseActivity中 - 您可以进行必要的编码以集成抽屉。扩展BaseActivity的每个活动现在将显示抽屉菜单(但实例将不同)

答案 4 :(得分:1)

现在您需要使用工具栏。 Android已经使 appcompat 库在旧版本的android中引入了材料设计。您需要使用appcompat的编译依赖项。 更改style.xml

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
</style>

</resources>

然后我们会选择没有旧的操作栏。之后我们在navigation drawer和工具栏之间签订合同。所以我们现在不需要使用旧的操作栏。 您可以参考以下代码:

<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#ff6d7fe2"
    app:contentInsetEnd="0dp"
    app:contentInsetStart="0dp"
    ></android.support.v7.widget.Toolbar>