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

Pro*_*nto 23 java android scrollview

我是一个Android开发人员.我也想使用ScrollView.This ScrollView需要一段时间禁用滚动和一些时间启用滚动.但我无法禁用滚动.如何实现它.请帮助我.我也尝试使用一些代码,如

fullparentscrolling.setHorizontalFadingEdgeEnabled(false);
fullparentscrolling.setVerticalFadingEdgeEnabled(false);
Run Code Online (Sandbox Code Playgroud)

要么

 fullparentscrolling.setEnabled(false);
Run Code Online (Sandbox Code Playgroud)

但它不起作用.

Bir*_*dia 67

试试这种方式

像这样创建您的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;
       }
}
}
Run Code Online (Sandbox Code Playgroud)

在你的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>
Run Code Online (Sandbox Code Playgroud)

在你的活动中

CustomScrollView myScrollView = (CustomScrollView) findViewById(R.id.myScroll);
        myScrollView.setEnableScrolling(false); // disable scrolling
        myScrollView.setEnableScrolling(true); // enable scrolling
Run Code Online (Sandbox Code Playgroud)


小智 25

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

mScrollView.setOnTouchListener( new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) 
            {
                  return true;
            }
});
Run Code Online (Sandbox Code Playgroud)

最适合所有人


Ham*_*mad 8

您无法禁用滚动浏览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);
    }

}
Run Code Online (Sandbox Code Playgroud)

为了进一步参考,这里是堆栈溢出的完整解决方案! 以编程方式禁用ScrollView?

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