ViewPager2 嵌套 ViewPager2或者 嵌套recyclerview,冲突问题解决方案

使用方法,使用NestedScrollableHost 包裹 子ViewPager2,代码如下,这样子viewpager2 也可以滑动了。滑动到最后,父viewPage2开始滑动。

 <xxxxxx.NestedScrollableHost
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent">
            <androidx.viewpager2.widget.ViewPager2
                android:id="@+id/viewPager2"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#f0f0f0"
                app:currIndex="@{item.currIndexPager}"
                app:itemBinding="@{item.itemBindingViewPager}"
                app:items="@{item.itemsViewPager}"
                app:onPageChangeCallback="@{item.onPageChangeCallback}"
                app:userScrollEnabled="@{true}" />

        </fly.component.widgets.NestedScrollableHost>

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewParent;
import android.widget.FrameLayout;

import java.util.HashMap;

import androidx.annotation.Nullable;
import androidx.viewpager2.widget.ViewPager2;

/**
 * 此类用于解决 ViewPager2  嵌套 ViewPager2 或者 RecyclerView 等相互嵌套的冲突问题,
 */
public class NestedScrollableHost extends FrameLayout {

    private int touchSlop;
    private float initialX;
    private float initialY;
    private HashMap _$_findViewCache;

    private final ViewPager2 getParentViewPager() {
        ViewParent var10000 = this.getParent();
        if (!(var10000 instanceof View)) {
            var10000 = null;
        }

        View v;
        for (v = (View) var10000; v != null && !(v instanceof ViewPager2); v = (View) var10000) {
            var10000 = v.getParent();
            if (!(var10000 instanceof View)) {
                var10000 = null;
            }
        }

        View var2 = v;
        if (!(v instanceof ViewPager2)) {
            var2 = null;
        }

        return (ViewPager2) var2;
    }

    private final View getChild() {
        return this.getChildCount() > 0 ? this.getChildAt(0) : null;
    }

    private final boolean canChildScroll(int orientation, float delta) {
        boolean var5 = false;
        int direction = -((int) Math.signum(delta));
        View var10000;
        boolean var6 = false;
        switch (orientation) {
            case 0:
                var10000 = this.getChild();
                var6 = var10000 != null ? var10000.canScrollHorizontally(direction) : false;
                break;
            case 1:
                var10000 = this.getChild();
                var6 = var10000 != null ? var10000.canScrollVertically(direction) : false;
                break;
            default:
                // throw (Throwable)(new IllegalArgumentException());
        }

        return var6;
    }

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

    private final void handleInterceptTouchEvent(MotionEvent e) {
        ViewPager2 var10000 = this.getParentViewPager();
        if (var10000 != null) {
            int orientation = var10000.getOrientation();
            if (this.canChildScroll(orientation, -1.0F) || this.canChildScroll(orientation, 1.0F)) {
                if (e.getAction() == 0) {
                    this.initialX = e.getX();
                    this.initialY = e.getY();
                    this.getParent().requestDisallowInterceptTouchEvent(true);
                } else if (e.getAction() == 2) {
                    float dx = e.getX() - this.initialX;
                    float dy = e.getY() - this.initialY;
                    boolean isVpHorizontal = orientation == 0;
                    boolean var8 = false;
                    float scaledDx = Math.abs(dx) * (isVpHorizontal ? 0.5F : 1.0F);
                    boolean var9 = false;
                    float scaledDy = Math.abs(dy) * (isVpHorizontal ? 1.0F : 0.5F);
                    if (scaledDx > (float) this.touchSlop || scaledDy > (float) this.touchSlop) {
                        if (isVpHorizontal == scaledDy > scaledDx) {
                            this.getParent().requestDisallowInterceptTouchEvent(false);
                        } else if (this.canChildScroll(orientation, isVpHorizontal ? dx : dy)) {
                            this.getParent().requestDisallowInterceptTouchEvent(true);
                        } else {
                            this.getParent().requestDisallowInterceptTouchEvent(false);
                        }
                    }
                }

            }
        }
    }

    public NestedScrollableHost(Context context) {
        super(context);
        ViewConfiguration var10001 = ViewConfiguration.get(this.getContext());
        this.touchSlop = var10001.getScaledTouchSlop();
    }

    public NestedScrollableHost(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        ViewConfiguration var10001 = ViewConfiguration.get(this.getContext());
        this.touchSlop = var10001.getScaledTouchSlop();
    }

    public View _$_findCachedViewById(int var1) {
        if (this._$_findViewCache == null) {
            this._$_findViewCache = new HashMap();
        }

        View var2 = (View) this._$_findViewCache.get(var1);
        if (var2 == null) {
            var2 = this.findViewById(var1);
            this._$_findViewCache.put(var1, var2);
        }

        return var2;
    }

    public void _$_clearFindViewByIdCache() {
        if (this._$_findViewCache != null) {
            this._$_findViewCache.clear();
        }

    }
}
  • 9
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值