最近公司的项目要实现一个类似Android版QQ的侧滑功能。查找了一下相关的资料发现GitHub上已经有类似的效果了,比较火的有ResideMenu和DragLayout,DragLayout相对来说简单不少,实现起来非常方便。GitHub地址:https://github.com/BlueMor/DragLayout。
要使用DragLayout只要把DragLayout和MyRelativeLayout这两个类复制到自己的项目中,在定义xml文件的时候将主界面和需要侧滑出的界面写在一起就行, 这里要注意最外层的Layout需要设为DragLayout:
<com.xxx.DragLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/dragLayout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</com.xxx.DragLayout>
主界面的Layout需要用 MyRelativeLayout包裹,顾名思义这里的MyRelativeLayout继承的是RelativeLayout,如果需要使用LinearLayout可以自行嵌套。
完整xml:
<yourpackagename.DragLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/dragLayout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="@+id/lin_mytime"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/lin_avatar"
android:layout_marginLeft="32dp"
android:layout_marginTop="60dp"
android:orientation="horizontal" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:src="@drawable/background" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:background="@color/transparent"/>
</LinearLayout>
<yourpackagename.MyRelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background" >
<include
android:id="@+id/topbar"
android:layout_width="match_parent"
android:layout_height="@dimen/topbar_height"
layout="@layout/main_topbar" />
<GridView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/topbar"
android:background="@drawable/background"
android:gravity="center"
android:numColumns="2"
android:stretchMode="columnWidth" >
</GridView>
</yourpackagename.MyRelativeLayout>
</yourpackagename.DragLayout>