如何在NestedScrollView中停止正在进行的滚动? [英] How to stop scroll in progress in NestedScrollView?

查看:212
本文介绍了如何在NestedScrollView中停止正在进行的滚动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个按钮可以自动返回NestedScrollView的顶部位置.当滚动不进行时,它就像一个超级按钮,否则,如果当前滚动仍在进行中,则滚动会移到顶部,但在...之后直接向下滚动一个亮的位.

I have a button to automatically returns to top position into a NestedScrollView. When the scroll is not in progress, it works like a charm, otherwise if the current scrolling is in progress yet, the scroll goes to top but directly scroll a lit bit down just after...

我写了这个方法:

private void scrollToTop() {
        mScrollView.stopNestedScroll();
        mScrollView.postDelayed(new Runnable() {
            @Override
            public void run() {
                mScrollView.scrollTo(0, 0);
            }
        }, 50);
    }

您有一些想法要停止当前滚动并立即转到顶部吗?

Have you got some ideas guys to stop the current scroll and go to top immediately?

非常感谢

推荐答案

正确的方法是实用滚动拦截用户的触摸.这将要求您扩展 ScrollView 来覆盖 scrollTo onInterceptTouchEvent 方法,并在列表为禁用用户触摸滚动,滚动完成后返回触摸.

The correct way would be intercepting user's touch when you scrolling pragmatically. That would require you to extend ScrollView overriding scrollTo and onInterceptTouchEvent methods and disable user touch while list is scrolling, returning touch after scroll is complete.

因此顺序如下:

  1. 调用 scrollTo 滚动到特定位置
  2. 启用标记以禁用触摸事件并开始滚动
  3. 滚动完成且状态更改为空闲禁用标志
  1. Call scrollTo to scroll to specific position
  2. Enable a flag to disable touch events and begin scrolling
  3. When scroll is finished and state changed to idle disable flag

P.S:我可以提供代码示例,但看来您在编写代码方面比我更有专长.干杯!

P.S: I can provide code example but it appears you have more expertise than me in writing codes. Cheers!

这是一个代码段:

import android.content.Context;
import android.support.v4.widget.NestedScrollView;
import android.util.AttributeSet;
import android.view.MotionEvent;

public class MyScrollView extends NestedScrollView {

    public static final int MAX_SCROLL_FACTOR = 1;
    boolean isAutoScrolling;

    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
    public void scrollTo(int x, int y) {
        isAutoScrolling = true;
        super.scrollTo(x, y);
    }


    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        if (isAutoScrolling)
            return super.onTouchEvent(event);

        return false;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (isAutoScrolling)
            return super.onTouchEvent(event);


        return false;
    }

    @Override
    protected void onScrollChanged(int x, int y, int oldX, int oldY) {
        super.onScrollChanged(x, y, oldX, oldY);
        if (isAutoScrolling) {
            if (Math.abs(y - oldY) < MAX_SCROLL_FACTOR  || y >= getMeasuredHeight() || y == 0
                    || Math.abs(x - oldX) < MAX_SCROLL_FACTOR || x >= getMeasuredWidth() || x == 0) {
                isAutoScrolling = false;
            }
        }
    }
}

这篇关于如何在NestedScrollView中停止正在进行的滚动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆