一、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);
}
}

③、初始化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);
}

ViewDragHelper的使用的更多相关文章

  1. Android -- ViewDragHelper

    ViewDragHelper SlidingPaneLayout和DrawerLayout,现在这俩个类被广泛的运用,其实研究他们的源码你会发现这两个类都运用了ViewDragHelper来处理拖动. ...

  2. Viewdraghelper解析

    2013年谷歌i/o大会上介绍了两个新的layout: SlidingPaneLayout和DrawerLayout,现在这俩个类被广泛的运用, 其实研究他们的源码你会发现这两个类都运用了ViewDr ...

  3. Android之ViewDragHelper

    在自定义ViewGroup中,很多效果都包含用户手指去拖动其内部的某个View(eg:侧滑菜单等),针对具体的需要去写好onInterceptTouchEvent和onTouchEvent这两个方法是 ...

  4. Android 一步一步教你使用ViewDragHelper

    在自定义viewgroup的时候 要重写onInterceptTouchEvent和onTouchEvent 这2个方法 是非常麻烦的事情,好在谷歌后来 推出了ViewDragHelper这个类.可以 ...

  5. Android ViewDragHelper源码解析

    在自定义ViewGroup的过程中,如果涉及到View的拖动滑动,ViewDragHelper的使用应该是少不了的,它提供了一系列用于用户拖动子View的辅助方法和相关的状态记录,像Navigatio ...

  6. Android 中 View移动总结:ViewDragHelper学习及用法详解

    如上图简单呈现出两个方块后,提出一个需求: 1.拖动方块时,方块(即子View)可以跟随手指移动. 2.一个方块移动时,另一个方块可以跟随移动. 3.将方块移动到左边区域(右边区域)后放开(即手指离开 ...

  7. ViewDragHelper详解

    2013年谷歌i/o大会上介绍了两个新的layout: SlidingPaneLayout和DrawerLayout,现在这俩个类被广泛的运用,其实研究他们的源码你会发现这两个类都运用了ViewDra ...

  8. ViewDragHelper练习使用

    转载博客地址:http://www.cnblogs.com/flyme2012/p/4076674.html 这个Demo是用来练习VIewDragHelper的,也是仿照网上一个大神的代码.我通过他 ...

  9. Android ViewDragHelper完全解析 自定义ViewGroup神器

    Android ViewDragHelper完全解析 自定义ViewGroup神器   转载请标明出处: http://blog.csdn.net/lmj623565791/article/detai ...

随机推荐

  1. 类和对象:面向对象编程 - 零基础入门学习Python037

    类和对象:面向对象编程 让编程改变世界 Change the world by program 经过上节课的热身,相信大家对类和对象已经有了初步的认识,但似乎还是懵懵懂懂:好像面向对象编程很厉害,但不 ...

  2. 1. Server.Transfer和Response.Redirect

    今天在使用ServerTransfer和Response.Redirect定位到当前页面来实现刷新页面时,发现了一些现象: 1.使用Response.Redirect刷新本页面,造成当前页面显示的数据 ...

  3. const int *p,int *const p区别(转)

    1)先从const int i说起.使用const修饰的i我们称之为符号常量.即,i不能在其他地方被重新赋值了.注意:const int i与int const i是等价的,相同的,即const与in ...

  4. Spark link集合

    Part1. 各种参数的意义及如何配置 Spark官方文档——Spark Configuration(Spark配置) http://www.cnblogs.com/vincent-hv/p/3316 ...

  5. cf B. Levko and Permutation

    http://codeforces.com/contest/361/problem/B #include <cstdio> #include <cstring> #includ ...

  6. 策略模式 - OK

    策略模式(Strategy):它定义了算法家族,分别封装起来,让他们之间可以互相替换,此模式让算法的变化不会影响到使用算法的客户. 策略模式是一种定义一系列算法的方法,从概念上来看,所有这些算法完成的 ...

  7. bash与sh的区别

    在shell脚本的开头往往有一句话来定义使用哪种sh解释器来解释脚本.目前研发送测的shell脚本中主要有以下两种方式:(1) #!/bin/sh(2) #!/bin/bash在这里求教同福客栈的各位 ...

  8. cf492B Vanya and Lanterns

    B. Vanya and Lanterns time limit per test 1 second memory limit per test 256 megabytes input standar ...

  9. 【转】打包AAC码流到FLV文件

    AAC编码后数据打包到FLV很简单.1. FLV音频Tag格式                              字节位置    意义0x08,                         ...

  10. [置顶] STM32移植contiki进阶之三(中):timer 中文版

    鉴于自己英语水平不高,在这里,将上一篇关于contiki 的timer的文章翻译为中文,让自己在学习的时候,更方便点.文中有许多不是很通顺的地方,将就吧. Timers Contiki系统提供了一套时 ...