Android仿IOS上拉/下拉弹性效果ScrollView

本文介绍了一种在Android中实现类似iOS滚动效果的方法。通过自定义一个名为IOSScrollView的组件,该组件继承自ScrollView,实现了当滚动到顶部或底部时仍可继续拉动并自动弹回的效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

效果图如下:
这里写图片描述

①实现原理:

Android自带的ScrollView滑动到顶部和底部后,就不能继续拖动了,因此要实现IOS的拉动弹性效果,可以自定义一个布局,继承ScrollView。

  1. 在最顶部时,可以向下拉动,并且弹回。
  2. 在最底部时,可以向上拉动,并且弹回。
  3. 不在最底部和最顶部时,就是默认的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

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值