Search code examples
androidandroid-viewpagerwikiseekbarswiperefreshlayout

Seekbar not scrolling smoothly inside viewpager


One fragment contains a viewpager having two fragments. In the first one the seekbar doesn't scroll but when trying to scroll slides the viewpager. It seems the viewpager offset gets activated while trying to scroll. But the problem is that I have same type configuration in another menu and it works. what is the solution for this?


Solution

  • I figured that the both fragment had one change. One had the swiperefreshlayout added and other didn't.

    After this, I figured the solution would something like below.

    If someone is still stuck with the question. Create a custom class of SwipeRefreshlayout and add to layout.

    public class CustomSwipeRefresh extends SwipeRefreshLayout {
    private int mTouchSlop;
    private float mPrevX;
    private boolean isDisabled;
    
    public CustomSwipeRefresh(Context context, AttributeSet attrs) {
        super(context, attrs);
        mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
    }
    
    public void disableInterceptTouchEvent(boolean isDisabled) {
        this.isDisabled = isDisabled;
        getParent().requestDisallowInterceptTouchEvent(isDisabled);
    }
    
    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mPrevX = MotionEvent.obtain(event).getX();
                break;
            case MotionEvent.ACTION_MOVE:
                if (isDisabled) {
                    return false;
                }
                float eventX = event.getX();
                float xDiff = Math.abs(eventX - mPrevX);
                if (xDiff > mTouchSlop) {
                    return false;
                }
        }
        return super.onInterceptTouchEvent(event);
        }
    }
    

    Then on your fragment class with seekbar, just add the code

    mswiperefresh.disableInterceptTouchEvent(boolean); to enable and disable swipe focus.

     @Override public void onStartTrackingTouch(SeekBar seekBar) {
         mswiperefresh.disableInterceptTouchEvent(true);
          }
    
     @Override
         public void onStopTrackingTouch(SeekBar seekBar) {
              mswiperefresh.disableInterceptTouchEvent(false);
        }