Android:Viewpager里面的SwipeRefreshLayout里面的搜索栏

时间:2015-11-21 08:45:16

标签: android android-viewpager touch swiperefreshlayout android-seekbar

我有一个包含Fragment数量的ViewPager。

在每个片段中,有一个SwipeRefreshLayout,它包含一个搜索栏。

但是,搜索条拇指不能平滑移动(水平方向)。

即使我仍然触摸拇指,拇指也会自动移动搜索栏和释放触摸的一定百分比。

有没有人遇到同样的情况?

怎么能解决它?

2 个答案:

答案 0 :(得分:1)

@robxyy回答是正确的,但我花了一些时间来弄清楚实现部分。因此,要详细说明他的答案。请参阅以下代码:

public class CustomSwipeRefreshLayout extends SwipeRefreshLayout {
    private int mTouchSlop;
    private float mPrevX;
    private boolean isDisabled;

    public CustomSwipeRefreshLayout(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);
    }
}

上面的类是扩展刷卡刷新布局。现在我们需要打电话

disableInterceptTouchEvent(true)

当我们启动Seekbar滑动时,我们需要调用

disableInterceptTouchEvent(false)

当Seekbar滑动停止时。因此,Custom Seekbar类看起来像:

public class SwipeView extends RelativeLayout implements SeekBar.OnSeekBarChangeListener {
    private CustomSwipeRefreshLayout swipeRefreshLayout;

    public SwipePromotionView(CustomSwipeRefreshLayout swipeRefreshLayout) {
        this.swipeRefreshLayout = swipeRefreshLayout;
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
          // include some logic here to decide which part of the swipe bar is a valid starting point
        swipeRefreshLayout.disableInterceptTouchEvent(true);
    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        swipeRefreshLayout.disableInterceptTouchEvent(false);
    }
}

答案 1 :(得分:0)

您可以扩展SwipeRefreshLayout,然后覆盖onInterceptTouchEvent方法。


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