Increasing touch priority of a SeekBar

拟墨画扇 提交于 2019-12-07 11:41:31

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)

Beowulf Bjornson

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);
        }
    });
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!