如何在Android ScrollView上禁用和启用滚动?

32

我是一名安卓开发者,我想使用ScrollView。这个ScrollView需要有时禁用滚动,有时启用滚动。但我无法禁用滚动。如何实现呢?请帮助我。我也尝试使用一些代码,例如

fullparentscrolling.setHorizontalFadingEdgeEnabled(false);
fullparentscrolling.setVerticalFadingEdgeEnabled(false);
或者
 fullparentscrolling.setEnabled(false);

但它不起作用。


你需要通过扩展scrollview来创建自定义的scrollview。 - Biraj Zalavadia
4个回答

84

尝试这种方式

像这样创建您的CustomScrollview

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ScrollView;

public class CustomScrollView extends ScrollView {

    private boolean enableScrolling = true;

    public boolean isEnableScrolling() {
        return enableScrolling;
    }

    public void setEnableScrolling(boolean enableScrolling) {
        this.enableScrolling = enableScrolling;
    }

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

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

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

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {

        if (isEnableScrolling()) {
            return super.onInterceptTouchEvent(ev);
        } else {
            return false;
        }
    }
    @Override
    public boolean onTouchEvent(MotionEvent ev) {
       if (isEnableScrolling()) {
            return super.onTouchEvent(ev);
       } else {
           return false;
       }
}
}

在你的xml文件中

//将"com.example.demo"替换为你的包名

<com.example.demo.CustomScrollView
        android:id="@+id/myScroll"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </com.example.demo.CustomScrollView>
在您的活动中。
CustomScrollView myScrollView = (CustomScrollView) findViewById(R.id.myScroll);
        myScrollView.setEnableScrolling(false); // disable scrolling
        myScrollView.setEnableScrolling(true); // enable scrolling

这对我的嵌套滚动视图情况也起作用,我需要在父滚动视图完全滚动到顶部之前禁用嵌套的scrollView。我的解决方案可以在这里查看:https://dev59.com/qJHea4cB1Zd3GeqPjwvo - worked

30

我为滚动视图设置了触摸侦听器,并在onTouch方法中返回了true。这对我很管用。

mScrollView.setOnTouchListener( new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) 
            {
                  return true;
            }
});

最佳选择


不要忘记使用“extends ScrollView”来继承ScrollView。 - Vlad.mir
1
另外,如果你想让ScrollView不消耗触摸事件 - return false - krossovochkin

11

你无法禁用 ScrollView 的滚动。你需要扩展 ScrollView 并重写 onTouchEvent 方法,在满足某些条件时返回 false。

你可以按照以下方式修改 ScrollView 来禁用滚动

class LockableScrollView extends ScrollView {

    ...

    // true if we can scroll (not locked)
    // false if we cannot scroll (locked)
    private boolean mScrollable = true;

    public void setScrollingEnabled(boolean enabled) {
        mScrollable = enabled;
    }

    public boolean isScrollable() {
        return mScrollable;
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                // if we can scroll pass the event to the superclass
                if (mScrollable) return super.onTouchEvent(ev);
                // only continue to handle the touch event if scrolling enabled
                return mScrollable; // mScrollable is always false at this point
            default:
                return super.onTouchEvent(ev);
        }
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        // Don't do anything with intercepted touch events if 
        // we are not scrollable
        if (!mScrollable) return false;
        else return super.onInterceptTouchEvent(ev);
    }

}

为了进一步参考,这里也提供了完整的Stack Overflow解决方案! 如何在程序中禁用ScrollView?

将此标记为答案以帮助其他人..谢谢


-11

试试这个,它会禁用滚动功能。

scrollview.setEnabled(false);

3
我也已经尝试过了,但它不起作用。 - Prosanto

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接