在 Android 中选择导航抽屉有什么含义?

What are the implications of choosing a Navigation Drawer in Android?

据我所知,在使用 Navigation Drawer (android.support.v4.widget.DrawerLayout) 时,您必须使用片段。

所以所有的活动都必须转化为片段。

此外,所有逻辑现在都将驻留在 Navigation Drawer 的 Activity 中,因此 Activities 文件的大小可能非常大并且不利于维护。

还有其他影响吗?是否有更简单的方法来解决这些问题?

我不久前用它制作了一个应用程序,我没有将所有活动转换为片段,而只是一种主菜单,然后当你从事一些 "different" activity 你没有一直抽屉直到你回来,甚至会很烦人。

逻辑我在Fragments里面放了一些(每个fragment相关的)维护。

拥有一个 activity 的原因很简单。这是因为您希望它保持持久性。

如果您有多个活动,它们都需要实现相同的抽屉布局,这会导致您多次膨胀和填充抽屉。

activity 只是作为片段和抽屉布局的容器。它就像两者之间的桥梁。

我相信你的概念是错误的。让我解释一下原因。

As far as I have seen when using a Navigation Drawer (android.support.v4.widget.DrawerLayout), you have to use Fragments. So All activities have to be transformed into fragments.

这是错误的。您可以只使用活动、片段或混合使用,您自己选择。

DrawerLayout只是一个ViewGroup。仅此而已。就像 FrameLayoutRelativeLayoutLinearLayout 等等。按照官方文档的定义:

ViewGroup is a special view that can contain other views (called children.)

这正是 DrawerLayout 的含义。 ViewGroup 公开了一个 "drawer like" 界面,可以用 swipe/drag 手势打开和关闭。

综上所述,您可以在网上找到的一些教程使用片段只是因为它们是构建 UI 组件的模块化方式。但是你也可以只使用普通视图,就像官方指南中的示例:

在这里你会看到一个 ListView 作为导航选项和一个 FrameLayout 当前 "main content" 将被放置在里面。

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

<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 -->
    <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>

编辑: 建议摘要 activity:

(全部用心打字,可能有错误,只是一般做法)

public abstract BaseActivity extends Activity implements AdapterView.OnItemClickListener {
   public void onCreate(Bundle savedInstances){
       super.onCreate(savedInstances);
       super.setContentView(R.layout.base_activity); // that's the layout above

      // do here the logic to setup the `ListView` and listen to OnItemClick for the list items

      listView.setOnItemClickListener(this);
   }

   @Override
   public void setContentView (int layoutResID){
      // here we override setContentView force content inside the FrameLayout
      LayoutInflater.from(this)
         .inflate(layoutResID,
                  findViewById(R.id.content_frame, true);
   }

   @Override
   public void onItemClick (AdapterView<?> parent, View view, int position, long id){
        // here you navigate to a different activity
   }
}

When using a NavigationDrawer you have to use Fragments.

  • 不正确。也可以有 Activitys。 但是,Fragments 应该代表更多的模块化 & 可重复使用 UI 块。

All logic will now reside in the Activity of the NavigationDrawer so the size of the Activitiy file could be really large and not maintenance friendly.

  • 如果你看一下 Google I/O 应用, 他们所做的是创建一个 class BaseActivity 来容纳所有 通用功能(包括 NavigationDrawer),然后是所有 其他 Activity 是从那个 class 扩展而来的。这样一来,所有 Activity 有许多共同特征,还有 NavigationDrawer, 但无需复制大量代码。

What are the implications of choosing a NavigationDrawer in Android?

  • 如上两点说明,可以用Activitys OR Fragments,并且可以将代码封装在可重用的 BaseActivity 中,这样相同的代码就不会被重新散列 到处。所以这意味着 而不是 NavigationDrawer 增加了 代码结构的复杂性。 NavigationDrawer 的目的是简化导航 否则管理起来既复杂又乏味,对用户来说都是如此 和程序员。