在滚动视图中使用搜索栏时出现问题

发布于 2025-01-07 18:26:32 字数 68 浏览 2 评论 0原文

我在滚动视图内有一个搜索栏。当有人在滚动滑块更改期间意外触摸滑块时。我只想在拖动拇指时更改滑块值。谁能帮我解决这个问题吗?

I have a seek bar inside a scroll view.When some one accidentally touches the slider during scrolling slider changes. I want to change the slider value only when the i drag the thumb. Can anyone help me with this?

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

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

发布评论

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

评论(1

GRAY°灰色天空 2025-01-14 18:26:32

我不知道有任何内置选项可以让您执行您想要的操作,但您可以轻松扩展默认的 SeekBar 实现并覆盖触摸事件的处理。基本实现看起来有点像这样:

public class ThumbOnlySeekBar extends SeekBar {

    private Drawable mThumb;

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

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

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

    @Override public void setThumb(Drawable thumb) {
        super.setThumb(thumb);
        mThumb = thumb;
    }

    @Override public boolean onTouchEvent(MotionEvent event) {
        if (!mThumb.getBounds().contains((int)event.getX(), (int)event.getY())) return true;
        return super.onTouchEvent(event);
}

}

由于 SeekBar 没有返回用于拇指的可绘制对象的 getThumb() 方法,因此有必要重写 setTumb(...) code> 来跟踪拇指集。您可以查找 SeekBar 的源代码,发现它总是很好地调用此方法来设置拇指可绘制 - 对我们来说很方便。 :)

触摸事件的处理基本上非常简单:只需测试触摸坐标是否在拇指可绘制的边界框内即可。显然,如果您有圆形拇指,您可以应用稍微不同的测试,但想法应该很清楚。如果触摸事件不在拇指上,则只需返回正在处理的事件并且不传播它。

I'm not aware of any builtin option that allow you to do what you're looking for, but you can easily extend the default SeekBar implementation and override the handling of touch events. A basic implementation will look somewhat like this:

public class ThumbOnlySeekBar extends SeekBar {

    private Drawable mThumb;

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

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

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

    @Override public void setThumb(Drawable thumb) {
        super.setThumb(thumb);
        mThumb = thumb;
    }

    @Override public boolean onTouchEvent(MotionEvent event) {
        if (!mThumb.getBounds().contains((int)event.getX(), (int)event.getY())) return true;
        return super.onTouchEvent(event);
}

}

As SeekBar does not have a getThumb() method that returns the drawable used for the thumb, it's necessary to override setTumb(...) to keep track of the thumb set. You can look up the source for SeekBar and see that it always nicely calls this method to set the thumb drawable - convenient for us. :)

The handling of touch events is basically very simple: just do a test whether the touch coordinates are inside the bounding box of the thumb drawable. Obviously, if you have circular thumb, you could apply a slightly different test, but the idea should be clear. If the touch event was not on the thumb, simply return it being handled and don't propagate it.

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