#爱加密杯#Navigation Drawer详解-Google推出的用来取代Sliding Menu的控件(三)

上一讲我们已经实现了在2.x的系统上同时使用ActionBar和Navigation Drawer,但是我们看一下google play的交互:
 

我们点击右上角的那个向下的小箭头,Navigation Drawer就会跳转到另外一个界面:
 

这个怎么实现呢?

不知道大家还记不记得我第一讲的内容:
用来显示屏幕主体内容的视图一般是 FrameLayout (运行的时候,会被一个 Fragment 填充),用来显示导航抽屉的视图一般是一个 ListView ,如下所示
  1. <android.support.v4.widget.DrawerLayout
  2. xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:id="@+id/drawer_layout"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent">
  6. <!-- The main content view -->
  7. <FrameLayout
  8. android:id="@+id/content_frame"
  9. android:layout_width="match_parent"
  10. android:layout_height="match_parent" />
  11. <!-- The navigation drawer -->
  12. <ListView android:id="@+id/left_drawer"
  13. android:layout_width="240dp"
  14. android:layout_height="match_parent"
  15. android:layout_gravity="start"
  16. android:choiceMode="singleChoice"
  17. android:divider="@android:color/transparent"
  18. android:dividerHeight="0dp"
  19. android:background="#111"/>
  20. </android.support.v4.widget.DrawerLayout>
复制代码

上面的布局说明了导航抽屉的布局一些非常重要的特点:
1 、显示主体内容的视图必须是 DrawerLayout 下的第一个子视图,因为抽屉视图必须在主体内容视图的上方(意味着 DrawerLayout 是一个以 z 轴来布局的控件)
2 、显示主体内容的视图必须设置为匹配父视图的高和宽,因为当抽屉视图隐藏的时候显示主体内容的视图代表了整个用户界面
3 、抽屉视图的 layout_gravity 属性值需为 “start” To support right-to-left (RTL) languages,specify the value with "start" insteadof "left" (so the drawer appears on the right when thelayout is RTL)
4 、抽屉视图的宽度不宜匹配父视图,应当适当的窄一点,这样就能在抽屉显示的时候还能看到主体内容视图的一部分



我们只要遵循这几条规则,就能写出自定义的ui了,下面我模仿google play,写了一个简单的layout:

  1. <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2.     android:id="@+id/drawer_layout"
  3.     android:layout_width="match_parent"
  4.     android:layout_height="match_parent" >

  5.     <FrameLayout
  6.         android:id="@+id/content_frame"
  7.         android:layout_width="match_parent"
  8.         android:layout_height="match_parent" />

  9.     <RelativeLayout
  10.         android:id="@+id/drawerLayout"
  11.         android:layout_width="240dp"
  12.         android:layout_height="match_parent"
  13.         android:background="#111"
  14.         android:layout_gravity="start"
  15.         >
  16.         <RelativeLayout
  17.             android:id="@+id/titleLayout"
  18.             android:layout_width="match_parent"
  19.                 android:layout_height="48dp"
  20.                 android:layout_alignParentTop="true"
  21.                 android:background="@color/abs__background_holo_dark"
  22.             >
  23.             <ImageView
  24.                 android:id="@+id/exchange_image"
  25.                         android:layout_width="wrap_content"
  26.                         android:layout_height="wrap_content"
  27.                         android:layout_centerVertical="true"
  28.                         android:layout_alignParentRight="true"
  29.                         android:background="@drawable/ic_more_arrow_down_dark"
  30.                             android:layout_marginRight="10dp"
  31.                 />
  32.         </RelativeLayout>
  33.         <RelativeLayout
  34.             android:id="@+id/content_layout"
  35.             android:layout_below="@id/titleLayout"
  36.                 android:layout_width="match_parent"
  37.                 android:layout_height="match_parent"
  38.             ></RelativeLayout>
  39.     </RelativeLayout>

  40. </android.support.v4.widget.DrawerLayout>
复制代码



布局中id为 drawerLayout 的布局,就是我们要加载的抽屉视图,大家注意要在这个layout中添加这条属性 android:layout_gravity="start"。


控制抽屉视图的跳转

我们首先给抽屉视图添加一个fragment:
  1.                 FragmentManager fm = getSupportFragmentManager();
  2.                 FragmentTransaction ft = fm.beginTransaction();
  3.                 ft.replace(R.id.content_layout, new DrawerFragment1(this));
  4.                 ft.commit();
复制代码



然后监听抽屉视图标题栏的点击事件:
  1.                 RelativeLayout titleLayout = (RelativeLayout) findViewById(R.id.titleLayout);
  2.                 titleLayout.setOnClickListener(this);
复制代码

我们在监听到用户的点击事件以后,替换fragment:
  1.                         if(showFirst) {
  2.                                 exchange_image.setBackgroundResource(R.drawable.ic_up_white_16);
  3.                                 
  4.                                 FragmentManager fm = getSupportFragmentManager();
  5.                                 FragmentTransaction ft = fm.beginTransaction();
  6.                                 ft.replace(R.id.content_layout, new DrawerFragment2());
  7.                                 ft.commit();
  8.                         } else {
  9.                                 exchange_image.setBackgroundResource(R.drawable.ic_more_arrow_down_dark);
  10.                                 
  11.                                 FragmentManager fm = getSupportFragmentManager();
  12.                                 FragmentTransaction ft = fm.beginTransaction();
  13.                                 ft.replace(R.id.content_layout, new DrawerFragment1(MainActivity.this));
  14.                                 ft.commit();
  15.                         }
  16.                         showFirst = !showFirst;
复制代码

下面是运行的截图:
 


点击右上角向下的小箭头以后:

 


这个时候就弹出了另外一个fragment。

源码下载:
本帖隐藏的内容
  workspace.rar (4.02 MB, 下载次数: 17)


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值