【问题标题】:Android Navigation Drawer implemented with Activities使用活动实现的 Android 导航抽屉
【发布时间】:2014-05-05 15:49:29
【问题描述】:

我正在开发自己的 Android 应用程序,我遇到了三个不同的活动,即活动 A、活动 B 和活动 C。我现在要做的是创建一个导航抽屉来在它们之间导航。我阅读了 Android 开发者网站上的教程,但他们只关注片段。如何仅使用一个 Activity 开发专业的 Android 应用程序,而使用 Fragments 开发所有其他屏幕?如果没有,为什么没有记录如何使用 Activity 来实现正确的导航抽屉?谢谢您的帮助。

【问题讨论】:

  • 请注意,文档说只有在拥有三个以上顶级视图的情况下才应该使用导航抽屉。
  • 好吧,更具体地说,Google Play 商店在导航抽屉方面是如何开发的?

标签: android android-fragments android-activity android-navigation


【解决方案1】:

您需要创建一个 Base activity 来执行所有常见的 Drawer navigation 内容。我将这个基础 Activity 称为 DrawerActivity ,所有其他 Activity 应该扩展这个 DrawerActivity 。所以所有的Activity 都会有一个Drawer Layout 的实例。

使用DrawerLayout 创建一个通用布局并将FrameLayoutListView 作为子级

  <android.support.v4.widget.DrawerLayout>
   <FrameLayout
    android:id="@+id/activity_frame”/>
   <ListView
    android:id="@+id/left_drawer”/>
 </android.support.v4.widget.DrawerLayout>

现在在DrawerActivity 上的onCreate() 中设置此布局

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_drawer_layout);
   // do other stuff to initialize drawer layout, add list items
  ……… 
   ……….
  // add a listener to the drawer list view 
 mLeftDrawerList.setOnItemClickListener(new DrawerItemClickListener());

}

添加项目点击监听器

  private class DrawerItemClickListener implements ListView.OnItemClickListener {
    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
        switch (position) {
            case 0: {
                Intent intent = new Intent(DrawerActivity.this, YourActivity.class);
                startActivity(intent);
                break;
            }
            default:
                break;
        }
        mDrawerLayout.closeDrawer(mLeftDrawerList);
    }
}

最后,所有其他活动都将扩展这个DrawerActivity

 public class MainActivity extends DrawerActivity {

   @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
   // don’t set any content view here, since its already set in DrawerActivity
   FrameLayout frameLayout = (FrameLayout)findViewById(R.id.activity_frame);
    // inflate the custom activity layout
    LayoutInflater layoutInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View activityView = layoutInflater.inflate(R.layout.activity_main, null,false);
    // add the custom layout of this activity to frame layout.
    frameLayout.addView(activityView);
    // now you can do all your other stuffs
    }
 }

你可以在这里看到完整的源代码https://gist.github.com/libinbensin/613dea436302d3015563

【讨论】:

  • 请提供扩展 DrawerActivity 的完整活动示例。我夸大了我的布局并将其添加为框架布局的子视图。但它不起作用。我只需要 (// 将此活动的自定义布局添加到框架布局。) ,如何正确地做到这一点。
  • 查看我的更新答案。您需要 inflate 您的 Activity 布局并将其作为子级添加到 FrameLayout。如果您仍然发现任何问题,请告诉我
  • 啊...我做了完全相同的代码,但我将 frameLayout 设置为 ViewGroup 参数以增加布局。可能这就是我的应用程序似乎陷入无限循环的原因。我会试试看,如果可行,那将是一个很大的帮助。非常感谢您的帮助。
  • 太棒了!谢谢!
  • @Libin 如何防止打开当前活动两次?
【解决方案2】:

您可以在每个 Activity 中拥有一个 NavigationDrawer,填充相同的选项列表。

【讨论】:

  • 我有想过,但这是一种常见的做法吗?
猜你喜欢
  • 1970-01-01
  • 2016-05-20
  • 1970-01-01
  • 2014-08-22
  • 2014-10-29
  • 1970-01-01
  • 1970-01-01
  • 2020-06-02
  • 1970-01-01
相关资源
最近更新 更多