ViewDragHelper的使用

一、ViewDragHelper的原理

   是一个能够自用移动ViewGroup内部View的控件。

   通过获取ViewGroup的点击事件,之后通过Scroller滑动来进行对ViewGroup内部控件的移动。

二、ViewDragHelper的作用

   ①、自由移动ViewGroup的内部控件

   ②、仿QQ的侧滑栏

   ③、拼图游戏啊之类的。

核心就是能够让View自由移动

三、制作简单的仿QQ的侧滑菜单

①、自定义一个Layout,因为ViewDragHelper是对ViewGroup的处理。

public class SlideMenuFrameLayout extends FrameLayout {
public SlideMenuFrameLayout(Context context) {
        super(context);
    }

    public SlideMenuFrameLayout(Context context, AttributeSet attrs{
        super(context, attrs);
    }

    public SlideMenuFrameLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
}
SlideMenuFrameLayout

创建好后,我们什么都不做。

之后在layout中创建SlideMenuFrameLayout的内部布局:

<com.cgx.sildingmenu.SlideMenuFrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.cgx.sildingmenu.MainActivity">

    <!--侧滑菜单-->
    <LinearLayout
        android:id="@+id/left_menu"
        android:layout_width="200dp"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:background="#000"
        android:layout_gravity="left"></LinearLayout>

    <!--主菜单-->
    <LinearLayout
        android:id="@+id/right_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:background="#fff"
        android:layout_gravity="right">
    </LinearLayout>
</com.cgx.sildingmenu.SlideMenuFrameLayout>
activity_main

在SlideMenuFrameLayout中添加侧滑Layout,和主Layout

②、初始化ViewDragHelper

首先:创建一个ViewDragHelper

mViewDragHelper = ViewDragHelper.create(this,null);//null代表暂不设置

其次:将点击事件交给mViewDragHelper:

@Override
public boolean onTouchEvent(MotionEvent event) {
mViewDragHelper.processTouchEvent(event);
return true;
}

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
return mViewDragHelper.shouldInterceptTouchEvent(ev);
}

最后:设置Scroller刷新:

@Overridepublic void computeScroll() {
super.computeScroll();
if (mViewDragHelper.continueSettling(true)){
ViewCompat.postInvalidateOnAnimation(this);
}
}
View Code

③、初始化ViewGroup的内部View也是是Menu和Conent

//当ViewGroup读取完layout中的xml的时候回调

@Override
protected void onFinishInflate() {
super.onFinishInflate();
mLinearMenu = (LinearLayout) getChildAt(0);
mLinearContent = (LinearLayout)getChildAt(1);
}

//当调用完onMeasure()方法的时候,调用该方法

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
mMenuWidth = mLinearMenu.getMeasuredWidth();
}

④、设置滑动事件:

首先:创建 ViewDragHelper.Callback callback = new ViewDragHelper.Callback();用来接收事件的回调

①、设定允许哪个View移动

@Override
public boolean tryCaptureView(View child, int pointerId) {
//允许LinearContent滑动
return mLinearContent == child;
}

②、设定滑动的距离

//允许左右移动
@Override
public int clampViewPositionHorizontal(View child, int left, int dx) {
return left;
}

③、设定当释放的时候menu是打开还是关闭

@Override
public void onViewReleased(View releasedChild, float xvel, float yvel) {
super.onViewReleased(releasedChild, xvel, yvel);
if (mLinearContent.getLeft() < mMenuWidth/2){
mViewDragHelper.smoothSlideViewTo(mLinearContent,0,0);
}
else {
mViewDragHelper.smoothSlideViewTo(mLinearContent,mMenuWidth,0);
}
ViewCompat.postInvalidateOnAnimation(SlideMenuFrameLayout.this);
}

 

posted @ 2016-08-07 11:49  技术丶从积累开始  阅读(207)  评论(0编辑  收藏  举报