Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

viewpager2嵌套xBanner滑动冲突问题 #219

Open
Dev-AHeng opened this issue Feb 27, 2022 · 3 comments
Open

viewpager2嵌套xBanner滑动冲突问题 #219

Dev-AHeng opened this issue Feb 27, 2022 · 3 comments

Comments

@Dev-AHeng
Copy link

xBanner写在viewpager2里, 然后xBanner就不能滑动了, 求大佬解决一下, 有点急!!

@KamiNoYuki
Copy link

没错,我也是viewPager2嵌套存在滑动冲突

@uaoan
Copy link

uaoan commented Jan 21, 2023

我也是,解决不了

@643063150
Copy link

没错,我也是viewPager2嵌套存在滑动冲突

`public class NestedScrollableHost extends FrameLayout {
private int touchSlop = 0;
private float initialX = 0f;
private float initialY = 0f;

public NestedScrollableHost(@NonNull Context context) {
    super(context);
    init(context);
}

public NestedScrollableHost(@NonNull Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
    init(context);
}

private void init(@NonNull Context context) {
    touchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
}

@Nullable
private ViewPager2 getParentViewPager() {
    ViewParent v = getParent();
    while (v != null && !(v instanceof ViewPager2)) {
        v = v.getParent();
    }
    return (ViewPager2) v;
}

@Nullable
private View getChild() {
    if (getChildCount() > 0) {
        if (getChildAt(0) instanceof XBanner) {
            return ((XBanner) getChildAt(0)).getViewPager();
        } else {
            return getChildAt(0);
        }
    } else {
        return null;
    }
}

private boolean canChildScroll(int orientation, float delta) {
    int direction = -(int) delta;
    View child = getChild();
    boolean result;
    switch (orientation) {
        case 0:
            result = child != null && child.canScrollHorizontally(direction);
            break;
        case 1:
            result = child != null && child.canScrollVertically(direction);
            break;
        default:
            throw new IllegalArgumentException();
    }
    return result;
}

@Override
public boolean onInterceptTouchEvent(@NonNull MotionEvent e) {
    handleInterceptTouchEvent(e);
    return super.onInterceptTouchEvent(e);
}

private void handleInterceptTouchEvent(@NonNull MotionEvent e) {
    ViewPager2 parentViewPager = getParentViewPager();
    if (parentViewPager == null) {
        return;
    }

    int orientation = parentViewPager.getOrientation();

    if (!canChildScroll(orientation, -1f) && !canChildScroll(orientation, 1f)) {
        return;
    }

    if (e.getAction() == MotionEvent.ACTION_DOWN) {
        initialX = e.getX();
        initialY = e.getY();
        getParent().requestDisallowInterceptTouchEvent(true);
    } else if (e.getAction() == MotionEvent.ACTION_MOVE) {
        float dx = e.getX() - initialX;
        float dy = e.getY() - initialY;
        boolean isVpHorizontal = orientation == ViewPager2.ORIENTATION_HORIZONTAL;

        float scaledDx = Math.abs(dx) * (isVpHorizontal ? .5f : 1f);
        float scaledDy = Math.abs(dy) * (isVpHorizontal ? 1f : .5f);

        if (scaledDx > touchSlop || scaledDy > touchSlop) {
            if (isVpHorizontal == (scaledDy > scaledDx)) {
                getParent().requestDisallowInterceptTouchEvent(false);
            } else {
                if (canChildScroll(orientation, isVpHorizontal ? dx : dy)) {
                    getParent().requestDisallowInterceptTouchEvent(true);
                } else {
                    getParent().requestDisallowInterceptTouchEvent(false);
                }
            }
        }
    }
}

}`
包裹住xbanner

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants