使用活动实现的 Android Navigation Drawer [英] Android Navigation Drawer implemented with Activities

查看:27
本文介绍了使用活动实现的 Android Navigation Drawer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am developing my own Android Application and i came to a point where i have three different Activities say Activity A, Activity B and Activity C. What i want to do now is to create a Navigation Drawer to navigate between them. I read the tutorial on the Android Developers website but they only focused on Fragments. How are professional Android Applications developed only with one Activity and all the other screens are developed with the use of Fragments?If not why isn't documented how to implement correct the navigation drawer with Activities instead?Thank you for your help.

推荐答案

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

You need to create a Base activity which does all the common Drawer navigation stuff . I will call this base Activity as DrawerActivity , and all other Activity should extend this DrawerActivity . So all the Activity will have one instance of Drawer Layout.

使用DrawerLayout 创建一个通用的Layout,并放置一个FrameLayoutListView 作为子元素

Create a common Layout with DrawerLayout and place a FrameLayout and ListView as child

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

现在在 DrawerActivity

 @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

Finally, All the other activity will extend this 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

You can see the complete source here https://gist.github.com/libinbensin/613dea436302d3015563

这篇关于使用活动实现的 Android Navigation Drawer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆