java - 如何禁用和启用 android ScrollView 上的滚动?

标签 java android scrollview

我是一名 android 开发人员。我也想使用 ScrollView。此 ScrollView 需要一些时间禁用滚动和一些时间启用滚动。但我无法禁用滚动。如何实现它。请帮助我。我也尝试使用一些代码,例如 s

fullparentscrolling.setHorizontalFadingEdgeEnabled(false);
fullparentscrolling.setVerticalFadingEdgeEnabled(false);

 fullparentscrolling.setEnabled(false);

但它不起作用。

最佳答案

试试这个方法

像这样创建您的 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>

在你的 Activity 中

CustomScrollView myScrollView = (CustomScrollView) findViewById(R.id.myScroll);
        myScrollView.setEnableScrolling(false); // disable scrolling
        myScrollView.setEnableScrolling(true); // enable scrolling

关于java - 如何禁用和启用 android ScrollView 上的滚动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18893198/

相关文章:

java - Android:带有单选按钮的 IF 语句不起作用

android - Retrofit LiveDataCallAdapter 不调用函数 adapt(call)

ios - 停止从 ScrollView 内的字段自动缩放

android - ImageView:如果 ImageView 不可见(在 ScrollView 内),则自动回收位图

java - Android gradle 构建失败,包含 firebase 和 gms 服务时出现异常

java - 来自服务器的字符串无法转换为 JSONObject?

java - 如何手动移动 GUI 元素?

安卓没有设置默认值

xcode - 如何使 scrollView 自动滚动?

java - 如何以编程方式向分区/系统写入内容?