效果图如下:
①实现原理:
Android自带的ScrollView滑动到顶部和底部后,就不能继续拖动了,因此要实现IOS的拉动弹性效果,可以自定义一个布局,继承ScrollView。
- 在最顶部时,可以向下拉动,并且弹回。
- 在最底部时,可以向上拉动,并且弹回。
- 不在最底部和最顶部时,就是默认的ScrollView的滑动效果。
如何判断ScrollView处于最顶部和最底部呢?
// 最顶部时,ScrollView的纵向滑动坐标值为0
getScrollY() == 0
// 最底部时,(ScrollView的子布局的高度 <= ScrollView纵向滑动坐标值 + ScrollView的高度)
contentView.getMeasuredHeight() <= getScrollY() + getHeight()
②具体代码如下:
/**
* 仿IOS上拉下拉弹性效果ScrollView
* @author yangmbin
* created at 2016/12/4 16:48
*/
public class IOSScrollView extends ScrollView {
// 上下文
private Context context;
// ScrollView子布局
private View contentView;
// 手势按下Y坐标
private float startY;
// 手势按下时,是否可以下拉/上拉标志
private boolean isCanPullDown, isCanPullUp;
// 保存ScrollView子布局的初始位置信息
private int leftPosition, topPosition, rightPosition, bottomPosition;
public IOSScrollView(Context context) {
super(context);
this.context = context;
}
public IOSScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
}
/**
* 获取ScrollView的子布局
*/
@Override
protected void onFinishInflate() {
if (getChildCount() > 0) {
contentView = getChildAt(0);
}
}
/**
* 获取初始子布局的位置信息
*/
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
if (contentView != null) {
leftPosition = contentView.getLeft();
topPosition = contentView.getTop();
rightPosition = contentView.getRight();
bottomPosition = contentView.getBottom();
}
}
/**
* 判断是否在ScrollView顶部,在顶部时可以下拉
*/
private boolean isScrollViewTop() {
if (getScrollY() == 0)
return true;
return false;
}
/**
* 判断是否在ScrollView底部,在顶部时可以上拉
*/
private boolean isScrollViewBottom() {
if (contentView.getMeasuredHeight() <= getScrollY() + getHeight())
return true;
return false;
}
/**
* 触摸事件处理
*/
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
if (contentView == null)
return super.dispatchTouchEvent(ev);
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
startY = ev.getY();
isCanPullDown = isScrollViewTop();
isCanPullUp = isScrollViewBottom();
break;
case MotionEvent.ACTION_UP:
float endY = ev.getY();
// 手势放开时,采用动画形式返回原位置
if (endY > startY && isCanPullDown || endY < startY && isCanPullUp) {
ObjectAnimator animator = ObjectAnimator.ofFloat(contentView, "translationY", contentView.getTop(), topPosition);
animator.setDuration(500);
animator.setInterpolator(new AccelerateInterpolator());
animator.start();
// 设置布局到正常位置
contentView.layout(leftPosition, topPosition, rightPosition, bottomPosition);
}
break;
case MotionEvent.ACTION_MOVE:
// 如果不在ScrollView的最顶部或最底部(startY需要是在最顶部或最底部时按下的坐标)
if (!isCanPullUp && !isCanPullDown) {
startY = ev.getY();
isCanPullDown = isScrollViewTop();
isCanPullUp = isScrollViewBottom();
break;
}
// 在最上部或最底部时,拉动移动布局
// 1、下拉 2、上拉 3、布局内容比ScrollView小,则既可以上拉,也可以下拉
if (isCanPullDown && ev.getY() > startY || isCanPullUp && ev.getY() < startY || isCanPullDown && isCanPullUp) {
int deltaY = (int) (ev.getY() - startY);
contentView.layout(leftPosition, topPosition + deltaY, rightPosition, bottomPosition + deltaY);
}
break;
}
return super.dispatchTouchEvent(ev);
}
}
XML中的使用方法:
<?xml version="1.0" encoding="utf-8"?>
<com.demo.IOSScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.demo.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="300dp"
android:text="111111111"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="300dp"
android:text="222222222"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="300dp"
android:text="333333333"/>
</LinearLayout>
</com.demo.IOSScrollView>
参考:http://blog.youkuaiyun.com/u014733374/article/details/42739345