【发布时间】:2014-03-21 12:28:04
【问题描述】:
我正在尝试寻找如何在导航抽屉中创建子菜单的解决方案。我将是来自 iOS 应用程序video 的最佳展示示例@
我在导航抽屉文档中没有看到任何方法来执行此类布局动画。我将不胜感激任何帮助
【问题讨论】:
我正在尝试寻找如何在导航抽屉中创建子菜单的解决方案。我将是来自 iOS 应用程序video 的最佳展示示例@
我在导航抽屉文档中没有看到任何方法来执行此类布局动画。我将不胜感激任何帮助
【问题讨论】:
在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();
}
所有其余部分的状态应该几乎相同.. 当然 - 文档中创建菜单列表和在活动中设置适配器的代码不再需要,因为它是在片段中实现的。
【讨论】: