ScrollView

布局

    <ScrollView
        android:id="@+id/sv"
        android:layout_width="368dp"
        android:layout_height="300dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp" android:layout_marginTop="8dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent">
        <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"
                      android:orientation="vertical">
            <TextView android:layout_width="match_parent" android:layout_height="100dp"
                      android:background="#F00"/>
            <TextView android:layout_width="match_parent" android:layout_height="100dp"
                      android:background="#0F0"/>
            <TextView android:layout_width="match_parent" android:layout_height="100dp"
                      android:background="#00F"/>
            <TextView android:layout_width="match_parent" android:layout_height="100dp"
                      android:background="#F00"/>
            <TextView android:layout_width="match_parent" android:layout_height="100dp"
                      android:background="#0F0"/>
            <TextView android:layout_width="match_parent" android:layout_height="100dp"
                      android:background="#00F"/>
            <TextView android:layout_width="match_parent" android:layout_height="100dp"
                      android:background="#F00"/>
            <TextView android:layout_width="match_parent" android:layout_height="100dp"
                      android:background="#0F0"/>
            <TextView android:layout_width="match_parent" android:layout_height="100dp"
                      android:background="#00F"/>
            <TextView android:layout_width="match_parent" android:layout_height="100dp"
                      android:background="#F00"/>
            <TextView android:layout_width="match_parent" android:layout_height="100dp"
                      android:background="#0F0"/>
            <TextView android:layout_width="match_parent" android:layout_height="100dp"
                      android:background="#00F"/>
        </LinearLayout>
    </ScrollView>

ScrollView的滑动事件监听

        sv.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                switch (motionEvent.getAction()){
                    case MotionEvent.ACTION_DOWN:

                        break;
                    case MotionEvent.ACTION_MOVE:
                        //判断当前滑动的y是否小于等于0,视为到顶部
                        if (sv.getScrollY()<=0){
                            Log.d("meee",getClass()+":\n"+"滑动到顶部");
                        }
                        //x y是像素,当sv.子控件的高度<=sv.height+sv.ScrollY时候,视为到底部
                        if(sv.getChildAt(0).getMeasuredHeight()<= sv.getScrollY() + sv.getHeight()){
                            Log.d("meee",getClass()+":\n"+"滑动到底部");
                        }
                        break;
                    case MotionEvent.ACTION_UP:
                    case MotionEvent.ACTION_CANCEL:
                        break;
                }
                return false;
            }
        });

滚动到底部或头部

        //获取焦点并滚动到底部,并返回是否成功的boolean
        button_left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
            //
                boolean b = sv.fullScroll(ScrollView.FOCUS_DOWN);
                Log.d("meee",getClass()+":\n"+b);
            }
        });
        //获取焦点并滚动到头部,并返回是否成功的boolean
        button_right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                boolean b = sv.fullScroll(ScrollView.FOCUS_UP);
                Log.d("meee",getClass()+":\n"+b);
            }
        });

代码控制滚动

//点击后直接闪到指定坐标
sv.scrollTo(0,x);
//平滑滚动到指定坐标
sv.smoothScrollTo(0,x);

滑动灵敏度控制

    public class MyScrollView extends ScrollView {

        public MyScrollView (Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
        }

        public MyScrollView (Context context, AttributeSet attrs) {
            super(context, attrs);
        }

        public MyScrollView (Context context) {
            super(context);
        }

        @Override
        public void fling(int velocityY) {
        //将灵敏度控制为原来的1/4;
            super.fling(velocityY / 4);
        }
    }

使用valueAnimator实现自动滚动效果

                //10秒内的时间滚动到底部
                ValueAnimator valueAnimator = ValueAnimator.ofInt(0, sv.getChildAt(0).getMeasuredHeight())
                        .setDuration(10*1000);
                //设置线性插值器
                valueAnimator.setInterpolator(new LinearInterpolator());
                valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                            @Override
                            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                                Integer animatedValue = (Integer) valueAnimator.getAnimatedValue();
                                sv.smoothScrollTo(0,animatedValue);
                            }
                        });
                //添加监听器
                valueAnimator.addListener(new Animator.AnimatorListener() {
                    long startTime;
                    @Override
                    public void onAnimationStart(Animator animator) {
                        startTime=System.currentTimeMillis();
                    }

                    @Override
                    public void onAnimationEnd(Animator animator) {
                        long processTime = System.currentTimeMillis() - startTime;
                        Toast.makeText(getApplicationContext(),"动画完毕processTime:"+processTime,Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onAnimationCancel(Animator animator) {
                    }

                    @Override
                    public void onAnimationRepeat(Animator animator) {
                    }
                });
                valueAnimator.start();
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值