一、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. (转载)C# 编程 使用可空类型

    转载出处为://https://msdn.microsoft.com/zh-cn/library/2cf62fcy(vs.90).aspx#Anchor_1 C#可以为 null 的类型可以表示基础类 ...

  2. C语言初学 计算二元一次方程的问题

    #include<stdio.h> #include<math.h> int main() { double a,b,c,disc,x1,x2; scanf("%lf ...

  3. C语言---volatile(我的工程笔记本)

    一般说来,volatile用在如下的几个地方: 1.中断服务程序中修改的供其它程序检测的变量需要加volatile: 2.多任务环境下各任务间共享的标志应该加volatile: 3.存储器映射的硬件寄 ...

  4. IOS流媒体播放

    IOS流媒体播放   1. 这里的流媒体地址是指服务端那边已经调好格式的可以在ios上播放的视频流. 下面提供几个视频流的地址: NSString *linkStr = http://61.160.2 ...

  5. Qt浅谈之二十App自动重启及关闭子窗口

    一.简介 最近因项目需求,Qt程序一旦检测到错误,要重新启动,自己是每次关闭主窗口的所有子窗口但有些模态框会出现问题,因此从网上总结了一些知识点,以备以后的应用. 二.详解 1.Qt结构 int ma ...

  6. Powershell 设置数值格式 1

    设置数值格式 1 6 6月, 2013  在 Powershell tagged 字符串 / 数字 / 文本 / 日期 / 格式化 by Mooser Lee 格式化操作符 -f 可以将数值插入到字符 ...

  7. github如何删除一个repository(仓库)

    昨天有人问我如何删除自己创建的一个项目,我浏览了一下github网站,确实不太容易找到删除功能.这里介绍一下啊如何在github上删除一个repository 一:首先点击进入你要删除的reposit ...

  8. logstash nginx 报ArgumentError: comparison of String with 5 failed

    80.82.78.38 [23/Sep/2016:05:36:18 +0800] "GET http://www.baidu.com/cache/global/img/gs.gif HTTP ...

  9. 传智播客8月C/C++基础班开班

     秋天已经向我们走来,在这个充满收获的季节里,大家齐聚传智C/C++学院这个大家庭,无论你曾经从事什么工作,都拥有着一颗热爱C/C++的心,为了自己心中的梦想,大家要付出百倍的努力,要做到&quo ...

  10. Oracle insert update 时间处理

    24小时表示方法:to_date(’ ::’,’yyyy-mm-dd hh24:mi:ss’) 12小时表示方法:to_date(’ ::’,’yyyy-mm-dd hh:mi:ss’) ','S75 ...