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);
}
}
③、初始化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的使用的更多相关文章
- Android -- ViewDragHelper
ViewDragHelper SlidingPaneLayout和DrawerLayout,现在这俩个类被广泛的运用,其实研究他们的源码你会发现这两个类都运用了ViewDragHelper来处理拖动. ...
- Viewdraghelper解析
2013年谷歌i/o大会上介绍了两个新的layout: SlidingPaneLayout和DrawerLayout,现在这俩个类被广泛的运用, 其实研究他们的源码你会发现这两个类都运用了ViewDr ...
- Android之ViewDragHelper
在自定义ViewGroup中,很多效果都包含用户手指去拖动其内部的某个View(eg:侧滑菜单等),针对具体的需要去写好onInterceptTouchEvent和onTouchEvent这两个方法是 ...
- Android 一步一步教你使用ViewDragHelper
在自定义viewgroup的时候 要重写onInterceptTouchEvent和onTouchEvent 这2个方法 是非常麻烦的事情,好在谷歌后来 推出了ViewDragHelper这个类.可以 ...
- Android ViewDragHelper源码解析
在自定义ViewGroup的过程中,如果涉及到View的拖动滑动,ViewDragHelper的使用应该是少不了的,它提供了一系列用于用户拖动子View的辅助方法和相关的状态记录,像Navigatio ...
- Android 中 View移动总结:ViewDragHelper学习及用法详解
如上图简单呈现出两个方块后,提出一个需求: 1.拖动方块时,方块(即子View)可以跟随手指移动. 2.一个方块移动时,另一个方块可以跟随移动. 3.将方块移动到左边区域(右边区域)后放开(即手指离开 ...
- ViewDragHelper详解
2013年谷歌i/o大会上介绍了两个新的layout: SlidingPaneLayout和DrawerLayout,现在这俩个类被广泛的运用,其实研究他们的源码你会发现这两个类都运用了ViewDra ...
- ViewDragHelper练习使用
转载博客地址:http://www.cnblogs.com/flyme2012/p/4076674.html 这个Demo是用来练习VIewDragHelper的,也是仿照网上一个大神的代码.我通过他 ...
- Android ViewDragHelper完全解析 自定义ViewGroup神器
Android ViewDragHelper完全解析 自定义ViewGroup神器 转载请标明出处: http://blog.csdn.net/lmj623565791/article/detai ...
随机推荐
- Python之简单工厂模式实现
最近又看了下大话设计模式,决定用Python来试着实现下. 基础类 class OperationBase(): """ 基础运算类 """ ...
- [linux]磁盘挂载
最近磁盘空间不足了, 所以需要将更多的磁盘空间加进来. 因为目前占空间最多的就是home, 无论是下载还是本地用户的东西都是放在这里的. 将分区格式化为ext4, 然后使用blkid /dev/sda ...
- 类和对象:继承 - 零基础入门学习Python038
类和对象:继承 让编程改变世界 Change the world by program 上节课的课后作业不知道大家完成的怎样?我们试图模拟一个场景,里边有一只乌龟和十条鱼,乌龟通过吃鱼来补充体力,当乌 ...
- Jasper_crosstab_display a value of field in crosstab total row
1.create a measure <measure name="myField" class="java.lang.String"> <m ...
- pfsense 2.2RC下的L2TP配置
还不有测试完成,不过,基本上应该差不多了. 主要参考以下文档: http://blog.sina.com.cn/s/blog_541a3cf10101ard3.html http://thepract ...
- leetcode_Substring with Concatenation of All Words
You are given a string, S, and a list of words, L, that are all of the same length. Find all startin ...
- BZOJ2212: [Poi2011]Tree Rotations
2212: [Poi2011]Tree Rotations Time Limit: 20 Sec Memory Limit: 259 MBSubmit: 391 Solved: 127[Submi ...
- 【剑指offer】面试题37:两个链表的第一个公共结点
题目: 输入两个链表,找出它们的第一个公共结点. 思路: 由链表的定义知是单链表.对于单链表,如果两个链表有公共结点,则两个链表必然是像Y型相交.则先计算出各个链表的长度,让长链表的头指针先走多出来的 ...
- 【hihoCoder第十六周】RMQ-ST算法
RMQ的大裸题.没什么意思.开始数组开小了,RE了一次.下面放代码. #include <bits/stdc++.h> using namespace std; vector<int ...
- C语言中所有变量和常量所使用的内存总结
(1)相同点:三种获取内存的方法,都可以给程序提供可用内存,都可以用来定义变量给程序用.(2)不同点:栈内存对应C中的普通局部变量(别的变量还用不了栈,而且栈是自动的,由编译器和运行时环境共同来提供服 ...