使用搜索栏滚动视图中的问题 [英] Issue using seekbar inside a scroll view

查看:151
本文介绍了使用搜索栏滚动视图中的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个搜索栏滚动内view.When有人滚动滑块改变时不慎沾到滑块。我想,当我拖到拇指只改变滑块值。谁能帮我这个?

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?

推荐答案

我不知道任何内建的选项,让你做你在找什么,但你可以很容易地扩展默认的搜索栏实现和覆盖处理触摸事件。基本实现会看起来有点像这样:

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);
}

}

由于搜索栏不具有返回用于拇指可绘制一个 getThumb()方法,它需要覆盖 setTumb(... )来跟踪拇指套。你可以看一下对搜索栏的来源,并认为它总是很好地调用此方法来设置拇指绘 - 方便了我们。 :)

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.

这篇关于使用搜索栏滚动视图中的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆