如何禁用 Horizo​​ntalScrollView

发布于 2025-01-20 20:32:02 字数 207 浏览 2 评论 0原文

我正在编写一个自定义 ChipGroup 类,在布局中我有一个 Horizo​​ntalScrollView 和一个 Material ChipGroup 。在我的构造函数中,我传递了一个名为 "isScrollable" 的布尔值,因为我希望它可以配置滚动。

如果滚动配置为 false,是否有办法禁用 Horizo​​ntalScrollView 的滚动?

I'm programming a custom ChipGroup class and in the layout I have a HorizontalScrollView and a Material ChipGroup inside. In my constructor I pass a boolean called "isScrollable" because I want it to be configurable the scrolling.

Is there a way to inactivate the scroll of the HorizontalScrollView if scrolling is configured as false?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

尝蛊 2025-01-27 20:32:02
  1. 您无法禁用 ScrollView 的滚动。你需要
    扩展ScrollView并重写onTouchEvent方法返回
    当某些条件匹配时为 false。
  2. Gallery 组件滚动
    水平方向,无论它是否在 ScrollView 中 - a
    ScrollView 仅提供垂直滚动(您需要一个
    Horizo​​ntalScrollView 用于水平滚动)。
  3. 你好像说你
    图像本身拉伸有问题——这没有任何问题
    对于 ScrollView,您可以更改 ImageView 的缩放方式
    使用 android:scaleType 属性 (XML) 或 setScaleType 方法
    例如 ScaleType.CENTER 不会拉伸您的图像,并将其以其原始大小居中。

您可以按如下方式修改 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
                return mScrollable && super.onTouchEvent(ev);
            default:
                return super.onTouchEvent(ev);
        }
    }

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

}

然后,

 <com.mypackagename.LockableScrollView 
        android:id="@+id/QuranGalleryScrollView" 
        android:layout_height="fill_parent" 
        android:layout_width="fill_parent">
    
        <Gallery android:id="@+id/Gallery" 
            android:layout_width="fill_parent" 
            android:layout_height="fill_parent"
            android:scrollbars="horizontal">
        </Gallery>
    
    </com.mypackagename.LockableScrollView>

在您的 XML 文件中(只需将 ScrollView 更改为特殊的 LockableScrollView)。

然后调用

((LockableScrollView)findViewById(R.id.QuranGalleryScrollView)).setScrollingEnabled(false);

以禁用视图的滚动。

  1. You cannot disable the scrolling of a ScrollView. You would need to
    extend to ScrollView and override the onTouchEvent method to return
    false when some condition is matched.
  2. The Gallery component scrolls
    horizontally regardless of whether it is in a ScrollView or not - a
    ScrollView provides only vertical scrolling (you need a
    HorizontalScrollView for horizontal scrolling).
  3. You seem to say you
    have a problem with the image stretching itself -- this has nothing
    to do with the ScrollView, you can change how an ImageView scales
    with the android:scaleType property (XML) or the setScaleType method
    for instance ScaleType.CENTER will not stretch your image and will center it at it's original size.

You could modify ScrollView as follows to disable scrolling:

  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
                return mScrollable && super.onTouchEvent(ev);
            default:
                return super.onTouchEvent(ev);
        }
    }

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

}

then,

 <com.mypackagename.LockableScrollView 
        android:id="@+id/QuranGalleryScrollView" 
        android:layout_height="fill_parent" 
        android:layout_width="fill_parent">
    
        <Gallery android:id="@+id/Gallery" 
            android:layout_width="fill_parent" 
            android:layout_height="fill_parent"
            android:scrollbars="horizontal">
        </Gallery>
    
    </com.mypackagename.LockableScrollView>

in your XML file (just changed the ScrollView to your special LockableScrollView).

Then call

((LockableScrollView)findViewById(R.id.QuranGalleryScrollView)).setScrollingEnabled(false);

to disable scrolling of the view.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文