自定义ScrollView 实现上拉下拉的回弹效果--并且子控件中有Viewpager的情况

onInterceptTouchEvent就是对子控件中Viewpager的处理:左右滑动应该让viewpager消费
public class MyScrollView extends ScrollView {
private View childView;
public MyScrollView(Context context) {
super(context);
}
public MyScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
// @Override
// protected void onLayout(boolean changed, int l, int t, int r, int b) {
// super.onLayout(changed, l, t, r, b);
// }
//获取子视图
@Override
protected void onFinishInflate() {
super.onFinishInflate();
if (getChildCount() > ) {
childView = getChildAt();
}
}
private int lastY;//上一次y轴方向操作的坐标位置
private Rect normal = new Rect();//用于记录临界状态的左、上、右、下
private boolean isFinishAnimation = true;//是否动画结束
private int lastX, downX, downY;
//拦截:实现父视图对子视图的拦截
//是否拦截成功,取决于方法的返回值。返回值true:拦截成功。反之,拦截失败
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
boolean isIntercept = false;
int eventX = (int) ev.getX();
int eventY = (int) ev.getY();
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
lastX = downX = eventX;
lastY = downY = eventY;
break;
case MotionEvent.ACTION_MOVE:
//获取水平和垂直方向的移动距离
int absX = Math.abs(eventX - downX);
int absY = Math.abs(eventY - downY);
if(absY > absX && absY >= UIUtils.dp2px()){
isIntercept = true;//执行拦截
}
lastX = eventX;
lastY = eventY;
break;
}
return isIntercept;
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
if (childView == null || !isFinishAnimation) {
return super.onTouchEvent(ev);
}
int eventY = (int) ev.getY();//获取当前的y轴坐标
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
lastY = eventY;
break;
case MotionEvent.ACTION_MOVE:
int dy = eventY - lastY;//微小的移动量
if (isNeedMove()) {
if (normal.isEmpty()) {
//记录了childView的临界状态的左、上、右、下
normal.set(childView.getLeft(), childView.getTop(), childView.getRight(), childView.getBottom());
}
//重新布局
childView.layout(childView.getLeft(), childView.getTop() + dy / , childView.getRight(), childView.getBottom() + dy / );
}
lastY = eventY;//重新赋值
break;
case MotionEvent.ACTION_UP:
if (isNeedAnimation()) {
//使用平移动画
int translateY = childView.getBottom() - normal.bottom;
TranslateAnimation translateAnimation = new TranslateAnimation(, , , -translateY);
translateAnimation.setDuration();
// translateAnimation.setFillAfter(true);//停留在最终位置上
translateAnimation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
isFinishAnimation = false;
}
@Override
public void onAnimationEnd(Animation animation) {
isFinishAnimation = true;
childView.clearAnimation();//清除动画
//重新布局
childView.layout(normal.left, normal.top, normal.right, normal.bottom);
//清除normal的数据
normal.setEmpty();
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
//启动动画
childView.startAnimation(translateAnimation);
}
break;
}
return super.onTouchEvent(ev);
}
//判断是否需要执行平移动画
private boolean isNeedAnimation() {
return !normal.isEmpty();
}
private boolean isNeedMove() {
int childMeasuredHeight = childView.getMeasuredHeight();//获取子视图的高度
int scrollViewMeasuredHeight = this.getMeasuredHeight();//获取布局的高度
Log.e("TAG", "childMeasuredHeight = " + childMeasuredHeight);
Log.e("TAG", "scrollViewMeasuredHeight = " + scrollViewMeasuredHeight);
int dy = childMeasuredHeight - scrollViewMeasuredHeight;//dy >= 0
int scrollY = this.getScrollY();//获取用户在y轴方向上的偏移量 (上 + 下 -)
if (scrollY <= || scrollY >= dy) {
return true;//按照我们自定义的MyScrollView的方式处理
}
//其他处在临界范围内的,返回false。即表示,仍按照ScrollView的方式处理
return false;
}
}
自定义ScrollView 实现上拉下拉的回弹效果--并且子控件中有Viewpager的情况的更多相关文章
- Android 自定义ScrollView 支持惯性滑动,惯性回弹效果。支持上拉加载更多
先讲下原理: ScrollView的子View 主要分为3部分:head头部,滚动内容,fooder底部 我们实现惯性滑动,以及回弹,都是靠超过head或者fooder 就重新滚动到 ,内容的顶部或 ...
- 解决iscroll.js上拉下拉刷新手指划出屏幕页面无法回弹问题
博客已迁移至http://zlwis.me. 使用过iscroll.js的上拉下拉刷新效果的朋友应该都碰到过这个问题:在iOS的浏览器中,上拉或下拉刷新时,当手指划出屏幕后,页面无法弹回.很多人因为解 ...
- 打造android万能上拉下拉刷新框架——XRefreshView (二)
打造Android万能上拉下拉刷新框架--XRefreshView(一) 打造Android万能上拉下拉刷新框架--XRefreshView(三) 一.前言 自从上次发表了打造android万能上拉下 ...
- iOS不得姐项目--推荐关注模块(一个控制器控制两个tableView),数据重复请求的问题,分页数据的加载,上拉下拉刷新(MJRefresh)
一.推荐关注模块(一个控制器控制两个tableView) -- 数据的显示 刚开始加载数据值得注意的有以下几点 导航控制器会自动调整scrollView的contentInset,最好是取消系统的设置 ...
- ListView实现上拉下拉刷新加载功能
第一步.首先在你项目中创建一个包存放支持下拉刷新和上拉加载的类:
- 打造Android万能上拉下拉刷新框架--XRefreshView(三)
转载请注明出处:http://blog.csdn.net/footballclub/ 打造Android万能上拉下拉刷新框架–XRefreshView(一) 打造Android万能上拉下拉刷新框架–X ...
- iOS不得姐项目--精华模块上拉下拉的注意事项,日期显示,重构子控制器,计算cell的高度(只计算一次),图片帖子的显示
一.上拉下拉注意事项 使用MJRefresh中的上拉控件自动设置透明 当请求下页数据通过page的时候,注意的是上拉加载更多数据失败的问题,下拉加载数据失败了,页数应该还原.或者是请求成功的时候再将页 ...
- 练习使用XRecyclerView,可上拉下拉刷新。
package com.lixu.testxrecyclerview; import android.support.v7.app.AppCompatActivity; import android. ...
- swift实现UItableview上拉下拉刷新模块
最近用写个项目 发现上拉下拉刷新模块没找到合适的 so 自己写了一个 由于最近忙 教程就不写了 里面有 直接贴地址https://github.com/DaChengTechnology/DCRefr ...
随机推荐
- MySQL备份---lvm snapshot
正常安装(缺点要锁表) 1, 创建一个LV(逻辑卷) , 把MySQL的数据目录放到这个LV上 /var/lib/mysql 对这个LV做快照, 从快照备份数据 删除快照 非正常安装 1,创建LV 2 ...
- zuul熔断代码
package com.sun.fallback; import java.io.ByteArrayInputStream; import java.io.IOException; import ja ...
- linux中变量的一些操作方法
常见的一般有如下操作,可以对字符串进行简单操作: echo ${#var}打印变量var长度 echo "$var:3:8" 打印变量var第4个字符开始的8个字符echo ${v ...
- linux中grep/egrep的使用
grep也是linux中查找的一个利器,运维.程序员必掌握的 下面针对grep的参数进行说明: --color 重点标记匹配到项grep "a word" datafile -- ...
- Volley Get网络请求
public class VolleyActivity extends AppCompatActivity { WebView webView; Button button; RequestQueue ...
- Linux_CentOS-服务器搭建 <五> 补充
O:文件的编码格式 1.文件转码问题 Windows中默认的文件格式是GBK(gb2312),而Linux一般都是UTF-8. 那么先说,如何查看吧.这时候强大的vi说,I can do that.( ...
- Springboot+Thymeleaf+layui框架的配置与使用
前言Springboot默认是不支持JSP的,默认使用thymeleaf模板引擎.所以这里介绍一下Springboot使用Thymeleaf的实例以及遇到的问题. 配置与使用1.在applicatio ...
- quartz配置参数org.quartz.jobStore.misfireThreshold含义解释
配置定时任务参数 quartz.properties文件时 需要配置jobStore的超过时间数 默认为60秒(这里单位为毫秒) org.quartz.jobStore.misfireThreshol ...
- Velocity常用语法详解
果然公司用的东西跟平时学的东西不太一样,我们公司前台页面并不是我们熟悉的.html或者.jsp文件,而是很多人不知道的 .vm文件,其实只要我们理解了jsp文件,vm文件也就是一些基本语法不同而已. ...
- CSS hack兼容表
IE6 IE7 IE8 Firefox Opera Safari !important Y Y Y Y Y _ Y * Y Y *+ Y \9 Y Y Y \0 Y nth-of-type(1) Y ...