【问题标题】:Sub menu in navigation drawer导航抽屉中的子菜单
【发布时间】:2014-03-21 12:28:04
【问题描述】:

我正在尝试寻找如何在导航抽屉中创建子菜单的解决方案。我将是来自 iOS 应用程序video 的最佳展示示例@

我在导航抽屉文档中没有看到任何方法来执行此类布局动画。我将不胜感激任何帮助

【问题讨论】:

标签: android navigation-drawer


【解决方案1】:

在android SDK 的导航抽屉中没有称为子菜单的概念。

但也有好消息 - 因为 Navigation Drawer 最终是布局容器 - 没有什么能阻止您在其中托管您想要的任何视图或嵌套布局容器,而不是仅在其中托管 ListView ,因此 - 您可以也可以使用Fragments

如果您这样做 - 您可以在它们之间执行片段事务以滑动到代表子菜单的另一个片段...如果您将其设置为,您还将“免费”获得片段事务动画使用一个。

每个片段将显示ListView(或其他任何......)代表不同的菜单屏幕

向它添加您自己的 UI 后退按钮标题并实现 onClick 回调以对“主菜单”片段执行片段事务,您就得到了您想要的。

这是您的主要活动 UI 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" />
<!-- The navigation drawer -->
<FrameLayout android:id="@+id/left_drawer_layout_fragment_conatiner"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:background="#111"/>

如您所见,我将traditional navigation drawer ListView 替换为FrameLayout。左抽屉可以替换为任何其他视图或您想要的自定义组件,也可以充当片段容器。

编辑

@Meryl 要求我根据documentation example 显示我如何使用Fragment 作为抽屉菜单。因此,假设您希望您的菜单看起来与文档示例完全一样(这根本不是必须的,因为您可以制作任何您想要的 UI ......)这将是将其转换为 Fragment 的方法:

fragment_menu.xml 布局文件:

<ListView android:id="@+id/list_view"
    xmlns:android="http://schemas.android.com/apk/res/android"        
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:choiceMode="singleChoice"
    android:divider="@android:color/transparent"
    android:dividerHeight="0dp"
    android:background="#111"/>

FragmentMenujava 类:

public class FragmentMenu extends Fragment {

private ListView mListView;

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle   savedInstanceState) {
    return inflater.inflate(R.layout.fragment_menu, null);
  }

  @Override
  public void onViewCreated(View view, Bundle savedInstanceState) {
      super.onViewCreated(view, savedInstanceState);

      mListView = (ListView)view.findViewById(R.id.list_view);

      // Set the adapter for the list view
      mListView.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_item, mPlanetTitles));
      // Set the list's click listener
      mListView.setOnItemClickListener(new DrawerItemClickListener());
  }
}

FragmentMenu 附加到Activity

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView...


    FragmentManager fragmentManager = getFragmentManager();
    fragmentManager.beginTransaction()
                   .replace(R.id.left_drawer_layout_fragment_conatiner, new FragmentMenu());
                   .commit();
}

所有其余部分的状态应该几乎相同.. 当然 - 文档中创建菜单列表和在活动中设置适配器的代码不再需要,因为它是在片段中实现的。

【讨论】:

  • 你的权利,这将是最简单的方法。谢谢
  • 您能否根据文档中的示例另外展示您如何使用片段作为菜单?
  • @Meryl:你去吧,我已经将它添加到我的答案中。但是 - 您要求这样做的事实意味着您对 ListView 和 Fragments 了解不多。我建议您学习这两个重要组件以充分利用它们
  • 谢谢,您添加的部分对我来说不是问题。我不知道如何处理 DrawerItemClickListener 类和 selectItem 方法。我将它们添加到片段,但我得到 java.lang.ClassCastException: android.widget.FrameLayout$LayoutParams cannot be cast to android.support.v4.widget.DrawerLayout$LayoutParams on mDrawerLayout.closeDrawer(mListView);那么
  • @Meryl:closeDrawer 方法期望获取显示在左侧抽屉中的根视图。因为它不再是 listView,而是框架布局 - 您应该将引用传递给该框架布局而不是 mListView。您可以像这样从主要活动中获取对框架的引用: View lefDrawerFrame = findViewById(R.id.left_drawer_layout_fragment_conatiner);
猜你喜欢
  • 1970-01-01
  • 2015-09-26
  • 2014-08-20
  • 1970-01-01
  • 2013-06-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多