- 浏览: 5776696 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (890)
- WindowsPhone (0)
- android (88)
- android快速迭代 (17)
- android基础 (34)
- android进阶 (172)
- android高级 (0)
- android拾遗 (85)
- android动画&效果 (68)
- Material Design (13)
- LUA (5)
- j2me (32)
- jQuery (39)
- spring (26)
- hibernate (20)
- struts (26)
- tomcat (9)
- javascript+css+html (62)
- jsp+servlet+javabean (14)
- java (37)
- velocity+FCKeditor (13)
- linux+批处理 (9)
- mysql (19)
- MyEclipse (9)
- ajax (7)
- wap (8)
- j2ee+apache (24)
- 其他 (13)
- phonegap (35)
最新评论
-
Memories_NC:
本地lua脚本终于执行成功了,虽然不是通过redis
java中调用lua脚本语言1 -
ZHOU452840622:
大神://处理返回的接收状态 这个好像没有监听到 遇 ...
android 发送短信的两种方式 -
PXY:
拦截部分地址,怎么写的for(int i=0;i<lis ...
判断是否登录的拦截器SessionFilter -
maotou1988:
Android控件之带清空按钮(功能)的AutoComplet ...
自定义AutoCompleteTextView -
yangmaolinpl:
希望有表例子更好。。。,不过也看明白了。
浅谈onInterceptTouchEvent、onTouchEvent与onTouch
先看一下图,有个直观的了解,向下拖动handle就“开门了”:
此DragLayout继承自LinearLayout,这样使得布局变的简单。
我把最顶部的View叫做HeadView,中间的叫“把手”HandleView,底部的叫ContentView,姑且这样叫着。
只有把手可以拖动,下面的ContentView不可以!
只要给DragLayout设置一个background,就会产生一个渐变显示背后布局的效果。
由于DragLayout继承自LinearLayout,所以背后的布局不属于DragLayout的范畴,
DragLayout只有三部分组成(HeadView,HandleView,ContentView)。
这样就造成了背后的布局不能响应手势事件(点击,拖动等),为什么?(该问题已经解决,见帖子最后部分)
因为使用ViewDragHelper需要重写onTouchEvent方法:
此方法必须返回true,消费掉了手势事件,所以背后的View就不会响应事件啦。
有无好办法让背后的View也响应事件???
我初步的构想:把背后的View也纳入到DragLayout布局中去,这样DragLayout就不应该继承LinearLayout,最好继承ViewGroup,这样布局的灵活度更高,当然也更繁碎,需要重写onLayout和onMeasure等方法。但至少可以解决背后View不能响应点击事件的问题!(我想应该是这样)
目前只能将就一下,给个初步的DragLayout实现源码:
测试代码:
测试布局:
整体的项目代码我不提供,有这三个就够了我想。
突然发现
解决背后的布局不能响应手势事件的问题很容易解决
只需要使用boolean isViewUnder = mDragHelper.isViewUnder(handleView, (int) x, (int) y);
判断一下就可以了
修改onTouchEvent()如下:
原来很简单的,之前还不知道此方法。
ViewDragHelper.CallBack中每个方法的用法
http://m.blog.csdn.net/blog/coder_nice/44678153
一个可以下滑显示出一个面板的Toolbar。这个library受Drawerlayout的启发,但有别于Drawerlayout显示左右抽屉,这个library会提供下拉toolbar显示一个面板的功能
http://www.jcodecraeer.com/a/opensource/2015/1204/3750.html
此DragLayout继承自LinearLayout,这样使得布局变的简单。
我把最顶部的View叫做HeadView,中间的叫“把手”HandleView,底部的叫ContentView,姑且这样叫着。
只有把手可以拖动,下面的ContentView不可以!
只要给DragLayout设置一个background,就会产生一个渐变显示背后布局的效果。
由于DragLayout继承自LinearLayout,所以背后的布局不属于DragLayout的范畴,
DragLayout只有三部分组成(HeadView,HandleView,ContentView)。
这样就造成了背后的布局不能响应手势事件(点击,拖动等),为什么?(该问题已经解决,见帖子最后部分)
因为使用ViewDragHelper需要重写onTouchEvent方法:
@Override public boolean onTouchEvent(MotionEvent ev) { mDragHelper.processTouchEvent(ev); return true; }
此方法必须返回true,消费掉了手势事件,所以背后的View就不会响应事件啦。
有无好办法让背后的View也响应事件???
我初步的构想:把背后的View也纳入到DragLayout布局中去,这样DragLayout就不应该继承LinearLayout,最好继承ViewGroup,这样布局的灵活度更高,当然也更繁碎,需要重写onLayout和onMeasure等方法。但至少可以解决背后View不能响应点击事件的问题!(我想应该是这样)
目前只能将就一下,给个初步的DragLayout实现源码:
import android.content.Context; import android.support.v4.view.MotionEventCompat; import android.support.v4.view.ViewCompat; import android.support.v4.widget.ViewDragHelper; import android.util.AttributeSet; import android.util.Log; import android.view.MotionEvent; import android.view.View; import android.widget.LinearLayout; /** * * @author pythoner * @since 2015-10-23 * */ public class DragLayout extends LinearLayout { private ViewDragHelper mDragHelper; private View headView,handleView,contentView;//三个控件 private int headViewHeight,handleViewHeight,contentViewHeight;//三个控件高度 private boolean isOpen=false; public DragLayout(Context context) { this(context, null); } public DragLayout(Context context, AttributeSet attrs) { this(context, attrs, 0); } public DragLayout(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); mDragHelper = ViewDragHelper.create(this, 1.0f, new DragHelperCallback()); setOrientation(LinearLayout.VERTICAL); } @Override protected void onFinishInflate() { super.onFinishInflate(); headView = findViewById(R.id.headView); handleView = findViewById(R.id.handleView); contentView = findViewById(R.id.contentView); } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { super.onLayout(changed, l, t, r, b); } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); headViewHeight = headView.getMeasuredHeight(); handleViewHeight = handleView.getMeasuredHeight(); contentViewHeight = contentView.getMeasuredHeight(); } @Override public boolean onInterceptTouchEvent(MotionEvent ev) { final int action = MotionEventCompat.getActionMasked(ev); if (action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_UP) { mDragHelper.cancel(); return false; } return mDragHelper.shouldInterceptTouchEvent(ev); } @Override public boolean onTouchEvent(MotionEvent ev) { mDragHelper.processTouchEvent(ev); return true; } @Override public void computeScroll() { super.computeScroll(); if (mDragHelper.continueSettling(true)) { ViewCompat.postInvalidateOnAnimation(this); } } class DragHelperCallback extends ViewDragHelper.Callback { @Override public boolean tryCaptureView(View child, int pointerId) { return child == handleView;//只有把手可以拖拽 } @Override public int clampViewPositionVertical(View child, int top, int dy) { // final int topBound = getPaddingTop(); //只在Y方向上可以拖动,且拖动范围介于HeadView之下 final int topBound = getPaddingTop() + headViewHeight; final int bottomBound = getHeight() - handleViewHeight; final int newTop = Math.min(Math.max(top, topBound), bottomBound); return newTop; } @Override public void onViewReleased(View releasedChild, float xvel, float yvel) { super.onViewReleased(releasedChild, xvel, yvel); //向下快速滑动或者向下滑动超过一半的ContentView时(慢速拖动的情况) if(yvel>0||releasedChild.getY()>headViewHeight+contentViewHeight/2){ mDragHelper.settleCapturedViewAt((int) handleView.getX(), getHeight() - handleViewHeight); if(onScrollListener!=null){ onScrollListener.onOpen(releasedChild); } isOpen=true; }else{ mDragHelper.settleCapturedViewAt((int) handleView.getX(), headViewHeight); if(onScrollListener!=null){ onScrollListener.onClose(releasedChild); } isOpen=false; } // 需要invalidate()以及结合computeScroll方法一起 invalidate(); } @Override public void onViewPositionChanged(View changedView, int left, int top, int dx, int dy) { super.onViewPositionChanged(changedView, left, top, dx, dy); headView.setY(headView.getY() - dy); // contentView.setY(contentView.getY()+dy);//此处不能使用setY(),否则拖动不上去了,why?所以采用了变通的layout()改变位置 contentView.layout(contentView.getLeft(), (int) (contentView.getY() + dy), contentView.getRight(), contentView.getBottom()); float fraction=(top-headViewHeight)*1.00f/contentViewHeight;//滑动位移比率 getBackground().setAlpha((int)(255*(1-fraction)));//背景根据滑动比率产生渐变效果 if(onScrollListener!=null){ onScrollListener.onScroll(changedView,fraction); } } } public boolean isOpen(){ return isOpen; } private OnScrollListener onScrollListener; public void setOnScrollListener(OnScrollListener onScrollListener) { this.onScrollListener = onScrollListener; } interface OnScrollListener{ void onScroll(View view,float fraction); void onOpen(View view); void onClose(View view); } }
测试代码:
import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); DragLayout dragLayout=(DragLayout)findViewById(R.id.dragLayout); dragLayout.setOnScrollListener(new DragLayout.OnScrollListener() { @Override public void onScroll(View view, float fraction) { // TODO Auto-generated method stub } @Override public void onOpen(View view) { // TODO Auto-generated method stub Log.i("tag", "============onOpen============"); } @Override public void onClose(View view) { // TODO Auto-generated method stub Log.i("tag", "============onClose============"); } }); TextView tv_head=(TextView)findViewById(R.id.tv_head); TextView tv_content=(TextView)findViewById(R.id.tv_content); tv_head.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Toast.makeText(MainActivity.this, "tv_head clicked", Toast.LENGTH_SHORT).show(); } }); tv_content.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Toast.makeText(MainActivity.this, "tv_content clicked", Toast.LENGTH_SHORT).show(); } }); } }
测试布局:
<FrameLayout 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" android:background="@android:color/holo_orange_dark" > <com.example.viewdraghelper.DragLayout android:id="@+id/dragLayout" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/background_dark" > <FrameLayout android:id="@+id/headView" android:layout_width="match_parent" android:layout_height="200dp" android:background="@android:color/holo_blue_bright" > <TextView android:id="@+id/tv_head" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:text="head" android:textSize="48sp" /> </FrameLayout> <TextView android:id="@+id/handleView" android:layout_width="match_parent" android:layout_height="64dp" android:background="@android:color/holo_blue_dark" android:gravity="center" android:text="handle" /> <FrameLayout android:id="@+id/contentView" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/holo_blue_bright" android:focusable="false" android:focusableInTouchMode="false" > <TextView android:id="@+id/tv_content" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:text="content" android:textSize="48sp" /> </FrameLayout> </com.example.viewdraghelper.DragLayout> </FrameLayout>
整体的项目代码我不提供,有这三个就够了我想。
突然发现
解决背后的布局不能响应手势事件的问题很容易解决
只需要使用boolean isViewUnder = mDragHelper.isViewUnder(handleView, (int) x, (int) y);
判断一下就可以了
修改onTouchEvent()如下:
@Override public boolean onTouchEvent(MotionEvent ev) { try { mDragHelper.processTouchEvent(ev); } catch (Exception e) { // TODO: handle exception } final float x = ev.getX(); final float y = ev.getY(); boolean isViewUnder = mDragHelper.isViewUnder(handleView, (int) x, (int) y); return isViewUnder; }
原来很简单的,之前还不知道此方法。
ViewDragHelper.CallBack中每个方法的用法
http://m.blog.csdn.net/blog/coder_nice/44678153
一个可以下滑显示出一个面板的Toolbar。这个library受Drawerlayout的启发,但有别于Drawerlayout显示左右抽屉,这个library会提供下拉toolbar显示一个面板的功能
http://www.jcodecraeer.com/a/opensource/2015/1204/3750.html
发表评论
-
NestedScrollView滚动到顶部固定子View悬停挂靠粘在顶端
2018-10-31 20:45 6800网上有一个StickyScrollView,称之为粘性Scro ... -
自定义Behavior实现AppBarLayout越界弹性效果
2017-03-31 09:33 10225一、继承AppBarLayout.Beha ... -
Android - 一种相似图片搜索算法的实现
2017-03-31 09:33 2568算法 缩小尺寸。 将图片缩小到8x8的尺寸,总共64个 ... -
使用SpringAnimation实现带下拉弹簧动画的 ScrollView
2017-03-30 11:30 2790在刚推出的 Support Library 25.3.0 里面 ... -
Android为应用添加角标(Badge)
2017-03-30 11:21 61011.需求简介 角标是什么意思呢? 看下图即可明了: 可 ... -
Android端与笔记本利用局域网进行FTP通信
2017-03-23 10:17 923先看图 打开前: 打开后: Activity类 ... -
PorterDuffColorFilter 在项目中的基本使用
2017-03-03 10:58 1299有时候标题栏会浮在内容之上,而内容会有颜色的变化,这时候就要求 ... -
ColorAnimationView 实现了滑动Viewpager 时背景色动态变化的过渡效果
2017-02-24 09:41 2170用法在注释中: import android.anima ... -
迷你轻量级全方向完美滑动处理侧滑控件SlideLayout
2017-01-16 16:53 2548纯手工超级迷你轻量级全方向完美滑动处理侧滑控件(比官方 sup ... -
Effect
2017-01-05 09:57 0https://github.com/JetradarMobi ... -
动态主题库Colorful,容易地改变App的配色方案
2016-12-27 14:49 2516Colorful是一个动态主题库,允许您很容易地改变App的配 ... -
对视图的对角线切割DiagonalView
2016-12-27 14:23 1071提供对视图的对角线切割,具有很好的用户定制 基本用法 ... -
仿淘宝京东拖拽商品详情页上下滚动黏滞效果
2016-12-26 16:53 3418比较常用的效果,有现成的,如此甚好!:) import ... -
让任意view具有滑动效果的SlideUp
2016-12-26 09:26 1659基本的类,只有一个: import android.a ... -
AdvancedWebView
2016-12-21 09:44 16https://github.com/delight-im/A ... -
可设置圆角背景边框的按钮, 通过调节色彩明度自动计算按下(pressed)状态颜色
2016-11-02 22:13 1860可设置圆角背景边框的的按钮, 通过调节色彩明度自动计算按下(p ... -
网络请求库相关
2016-10-09 09:35 62https://github.com/amitshekhari ... -
ASimpleCache一个简单的缓存框架
2015-10-26 22:53 2121ASimpleCache 是一个为android制定的 轻量级 ... -
保证图片长宽比的同时拉伸图片ImageView
2015-10-16 15:40 3677按比例放大图片,不拉伸失真 import android. ... -
向上拖动时,可以惯性滑动显示到下一页的控件DragLayout
2015-10-16 14:53 5527仿照淘宝和聚美优品,在商品详情页,向上拖动时,可以加载下一页。 ...
相关推荐
一个使用ViewDragHelper来实现的 安卓抽屉式滑动效果
主要为大家详细介绍了ViewDragHelper实现QQ侧滑效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
Android DragLayout仿QQ侧滑效果使用support.v4包下的ViewDragHelper实现QQ5.0侧滑效果。
DOWN,ACTION_MOVE,ACTION_UP事件来处理相应的拖拽效果,这里采用ViewDragHelper的方式去实现拖拽,顺便学习了一下ViewDragHelper的使用方式,拖拽时的粘连效果采用贝塞尔曲线来实现。 用ViewDragHelper实现拖拽效果 ...
主要介绍了Android程序开发实现QQ6.X最新版本侧滑界面效果实例代码的相关资料,需要的朋友可以参考下
使用ViewDragHelper,仿照豆瓣音乐效果。项目地址:https://github.com/BCsl/ViewDragHelperDemo 效果图:
SwipeLayout 利用ViewDragHelper优雅实现侧滑删除功能,代码简洁,效果不错,适应于任何View
ViewDragHelper 实现的侧拉滑动效果,可以用来学习
仿照QQ最新版的侧滑效果demo,可以直接运行,最好在真机上运行,因为demo中读取的是本地的图片。不多说了自己看效果!
Android ViewDragHelper 实现 QQ5.0 侧滑 (9月25日修改代码) http://blog.csdn.net/jdsjlzx/article/details/41210659
用ViewDragHelper实现View拖动,可实现浮动窗口等。
主要介绍了Android ViewDragHelper实现京东、淘宝拖拽详情,实现这种效果大概分为三种方式,具体哪三种方式大家通过本文了解下吧
主要介绍了Android使用ViewDragHelper实现仿QQ6.0侧滑界面(一)的相关资料,需要的朋友可以参考下
主要介绍了Android使用ViewDragHelper实现图片下拽返回示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
基于viewdraghelper实现的下拉刷新组件,集成了下拉刷新,底部加载更多,数据初始加载显示loading等功能。
介绍:用ViewDragHelper实现的activity切换动画。运行效果:使用说明: 你可以将这个库当成view来用:将DraggerView添加到root layout,并且在里面加入两个layout。 android:layout_width="match_parent" ...