Search code examples
androidscrollviewseekbar

How to disable work of ScrollView on inner SeekBar?


For example, when I move the circleseekbar slider, the scrollview also works, thereby moving the entire layout up, and I just need to be able to change the position of the slider.

Tried it through android:scrollbars = "none" - everything also failed.


Solution

  • You need to temporary stop the ScrollView scrolling and re-enable it when you're done manipulating the SeekBar.

    To achieve that: Instead of using ScrollView, use the below customized one where Overriding onInterceptTouchEvent did the trick.

    public class LockableScrollView extends ScrollView {
    
        // true if we can scroll (not locked)
        // false if we cannot scroll (locked)
        private boolean mScrollable = true;
    
        public LockableScrollView(Context context) {
            super(context);
        }
    
        public LockableScrollView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public LockableScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
        }
    
        public void setScrollingEnabled(boolean enabled) {
            mScrollable = enabled;
        }
    
        public boolean isScrollable() {
            return mScrollable;
        }
    
        @SuppressLint("ClickableViewAccessibility")
        @Override
        public boolean onTouchEvent(MotionEvent ev) {
            if (ev.getAction() == MotionEvent.ACTION_DOWN) {// if we can scroll pass the event to the superclass
                return mScrollable && super.onTouchEvent(ev);
            }
            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);
        }
    }
    

    You can temporary stop the scrolling when you touch the SeekBar by calling myScrollView.setScrollingEnabled(false); and enable it when you lift the finger off the Seekbar as below

    seekBar.setOnTouchListener(new View.OnTouchListener() {
    
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN)
                myScrollView.setScrollingEnabled(false);
            else if (event.getAction() == MotionEvent.ACTION_UP)
                myScrollView.setScrollingEnabled(true);
            return false;
        }
    });
    

    Reference of LocableScrollView