android 禁止scrollview横向滚动,如何禁用和启用android ScrollView上的滚动?

本文介绍了一个自定义的ScrollView——CustomScrollView,通过设置enableScrolling属性来控制滚动视图的开关。在XML布局中使用该自定义组件,并在活动中通过myScrollView.setEnableScrolling()方法轻松实现滚动功能的开启或关闭。
摘要由CSDN通过智能技术生成

小编典典

试试这个

像这样创建您的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”替换为您的包名称

android:id="@+id/myScroll"

android:layout_width="match_parent"

android:layout_height="wrap_content" >

在您的活动中

CustomScrollView myScrollView = (CustomScrollView) findViewById(R.id.myScroll);

myScrollView.setEnableScrolling(false); // disable scrolling

myScrollView.setEnableScrolling(true); // enable scrolling

2020-09-26

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值