增加 SeekBar 的触摸优先级

发布于 2024-12-28 20:20:24 字数 219 浏览 2 评论 0原文

我在 Horizo​​ntalScrollView 中有一个 SeekBar,并且发现很难抓住手柄并移动它。如果我没有准确地触摸手柄,Horizo​​ntalScrollView 将接收并使用触摸事件。有没有办法提高 SeekBar 的优先级,以便它具有触摸事件的正常“范围”,或者这只是一个坏主意/设计,我应该避免它?

I have a SeekBar inside a HorizontalScrollView and am finding it very hard to grab the handle and move it. If I don't touch exactly on the handle, the HorizontalScrollView will receive and consume the touch event. Is there a way to increase the priority of the SeekBar so it will have the normal "range" for touch events or is this just a bad idea/design and I should avoid it?

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

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

发布评论

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

评论(2

究竟谁懂我的在乎 2025-01-04 20:20:24

这是解决此问题的另一种方法,无需扩展框架布局。
这个想法是在触摸 SeekBar 时禁用 Horizo​​ntalScrollView 的触摸事件。
因此,例如,当您初始化视图时,您可以定义如下内容:

protected void onCreate(final Bundle savedInstanceState)
{

    [...]

    final HorizontalScrollView scrollView = (HorizontalScrollView) findViewById(R.id.vo_cw_hsv);
    final SeekBar opacitySeekBar = (SeekBar) findViewById(R.id.vo_cw_seekbar);
    opacitySeekBar.setOnTouchListener(new OnTouchListener()
    {
      @Override
      public boolean onTouch(final View view, final MotionEvent event)
      {
        if (event.getAction() == MotionEvent.ACTION_DOWN || event.getAction() == MotionEvent.ACTION_MOVE)

          scrollView.requestDisallowInterceptTouchEvent(true);

        return false;
      }
    });

    [...]

}

有关详细信息,您可以阅读 ViewParent 类上的文档:
http://developer.android.com/reference/android /view/ViewParent.html#requestDisallowInterceptTouchEvent(boolean)

Here is an other way to fix this issue without extends a Frame layout.
The idea is to disable the touch event of the HorizontalScrollView when you touch the SeekBar.
So, when you initialize the view for example, you can define something like this:

protected void onCreate(final Bundle savedInstanceState)
{

    [...]

    final HorizontalScrollView scrollView = (HorizontalScrollView) findViewById(R.id.vo_cw_hsv);
    final SeekBar opacitySeekBar = (SeekBar) findViewById(R.id.vo_cw_seekbar);
    opacitySeekBar.setOnTouchListener(new OnTouchListener()
    {
      @Override
      public boolean onTouch(final View view, final MotionEvent event)
      {
        if (event.getAction() == MotionEvent.ACTION_DOWN || event.getAction() == MotionEvent.ACTION_MOVE)

          scrollView.requestDisallowInterceptTouchEvent(true);

        return false;
      }
    });

    [...]

}

For more information you can read the doc on ViewParent class:
http://developer.android.com/reference/android/view/ViewParent.html#requestDisallowInterceptTouchEvent(boolean)

背叛残局 2025-01-04 20:20:24

我通过执行以下操作“解决”了它:

注意:我的滚动布局是可锁定的,如下所述:https ://stackoverflow.com/a/5763815/1067721 (只需注意我的答案稍微低一点,我发现这一点很重要)

然后我将我的搜索栏包裹在能够拦截触摸事件的 FrameLayout:

public class InterceptFrameLayout extends FrameLayout {

    private OnTouchListener mListener;

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

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

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

    @Override
    public boolean onInterceptTouchEvent (MotionEvent ev) {
        return onTouchEvent(ev);
    }

    @Override
    public void setOnTouchListener (View.OnTouchListener l) {
        mListener = l;
    }

    @Override
    public boolean onTouchEvent (MotionEvent ev) {
        if (mListener != null) {
            return mListener.onTouch(this, ev);
        } else {
            return super.onTouchEvent(ev);
        }
    }
}

然后,在我的 Fragment 上,我实现了侦听器并让 SeekBar 消耗该事件:

    volWrapper.setOnTouchListener(new OnTouchListener() {           
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN: {
                    mHostingScroll.setScrollable(false);
                    break;
                }
                case MotionEvent.ACTION_UP: {
                    mHostingScroll.setScrollable(true);
                    break;
                }
            }
            return volumeBar.onTouchEvent(event);
        }
    });

I "solved" it by doing the following:

NOTE: My scroll layout is lockable, as explained here: https://stackoverflow.com/a/5763815/1067721 (just pay atention to my answer a little bit lower, I found that bit to be important)

I then wrapped my seekbar in a FrameLayout able to intercept touch events:

public class InterceptFrameLayout extends FrameLayout {

    private OnTouchListener mListener;

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

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

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

    @Override
    public boolean onInterceptTouchEvent (MotionEvent ev) {
        return onTouchEvent(ev);
    }

    @Override
    public void setOnTouchListener (View.OnTouchListener l) {
        mListener = l;
    }

    @Override
    public boolean onTouchEvent (MotionEvent ev) {
        if (mListener != null) {
            return mListener.onTouch(this, ev);
        } else {
            return super.onTouchEvent(ev);
        }
    }
}

And then, on my Fragment, I implemented the listener and let the SeekBar consume the event:

    volWrapper.setOnTouchListener(new OnTouchListener() {           
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN: {
                    mHostingScroll.setScrollable(false);
                    break;
                }
                case MotionEvent.ACTION_UP: {
                    mHostingScroll.setScrollable(true);
                    break;
                }
            }
            return volumeBar.onTouchEvent(event);
        }
    });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文