仿QQ侧滑效果,DragLayout使用方法

本文介绍如何使用DragLayout库轻松实现Android版QQ的侧滑功能,包括XML布局配置和类引用,适合开发者快速上手。

    最近公司的项目要实现一个类似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>

DragLinearLayout是一个LinearLayout, 他可是其子View在其范围内可拖动、可交换位置。默认情况下,子View是不可拖动的,你需要调用DragLinearLayout.setViewDraggable(child, child)方法让其可拖动。项目地址:https://github.com/justasm/DragLinearLayout 效果图:如何使用使用LinearLayout一样:                        2. 让子View可拖动默认情况下子View是不可拖动的,你需要调用dragLinearLayout.setViewDraggable()让子View变为可拖动的。DragLinearLayout dragLinearLayout = (DragLinearLayout) findViewById(R.id.container); //让子view可拖动,默认是不能拖动的         for(int i = 1; i < dragLinearLayout.getChildCount(); i ){             View child = dragLinearLayout.getChildAt(i);             dragLinearLayout.setViewDraggable(child, child);          }可以动态添加可拖动子viewfinal View view = View.inflate(context, R.layout.view_layout, null); dragLinearLayout.addDragView(view, view.findViewById(R.id.view_drag_handle));   // ..   dragLinearLayout.removeDragView(view);使用OnViewSwapListener检测子view之间的排序变化事件:dragLinearLayout.setOnViewSwapListener(new DragLinearLayout.OnViewSwapListener() {     @Override     public void onSwap(View firstView, int firstPosition,             View secondView, int secondPosition) {         // update data, etc..     } });当在ScrollView中使用DragLinearLayout的时候,如果你想在拖拽的时候ScrollView也能滚动,需要调用setContainerScrollView(Scroll
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值