当前位置:  开发笔记 > Android > 正文

CoordinatorLayout在另一个CoordinatorLayout里面

如何解决《CoordinatorLayout在另一个CoordinatorLayout里面》经验,为你挑选了3个好方法。

CorodinatorLayout在另一个内部CoordinatorLayout,滚动子视图也应该滚动父CoordinatorLayout.

我有一个coordinatorLayoutViewPager包含不同Fragment,使得在Scroll将隐藏tabLayout

我有另一个coordinatorLayoutviewPager.这fragmentViewPager父片段(父母Coordinator layout)的膨胀.

问题是onScrolling子片段childViewpager只反映在coordinator layout孩子fragment而不是父母coordinator layout,我需要做的是隐藏tablayout.

结构是:

CoordinatorLayout(p) ->(tablayout(p) & ViewPager(p) -> CoordinatorLayout(c)  ->(tablayout(c) & ViewPAger(c) ->recyclerView(cc)))

p -> parent;

c -> child; cc -> child to child

如何在滚动回收器视图上进行操作将影响协调器布局,以便工具栏tablayout(p)将被隐藏.



1> Fabian..:

我知道这是一个老问题.但是我搜索了很长时间才将CoordinatorLayout一个片段包含在另一个片段中CoordinatorLayout.

我稍微修改了dev.bmax的答案,调用两个协调器布局并调用两个布局的附加行为.

所以这是我的解决方案.

@SuppressWarnings("unused")
public class NestedCoordinatorLayout extends CoordinatorLayout implements NestedScrollingChild {

    private NestedScrollingChildHelper mChildHelper;

    public NestedCoordinatorLayout(Context context) {
        super(context);
        mChildHelper = new NestedScrollingChildHelper(this);
        setNestedScrollingEnabled(true);
    }

    public NestedCoordinatorLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        mChildHelper = new NestedScrollingChildHelper(this);
        setNestedScrollingEnabled(true);
    }

    public NestedCoordinatorLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mChildHelper = new NestedScrollingChildHelper(this);
        setNestedScrollingEnabled(true);
    }

    @Override
    public boolean onStartNestedScroll(View child, View target, int     nestedScrollAxes) {
        /* Enable the scrolling behavior of our own children */
        boolean tHandled = super.onStartNestedScroll(child, target, nestedScrollAxes);
        /* Enable the scrolling behavior of the parent's other children  */
        return startNestedScroll(nestedScrollAxes) || tHandled;
    }

    @Override
    public void onStopNestedScroll(View target) {
        /* Disable the scrolling behavior of our own children */
        super.onStopNestedScroll(target);
        /* Disable the scrolling behavior of the parent's other children  */
        stopNestedScroll();
    }

    @Override
    public void onNestedPreScroll(View target, int dx, int dy, int[] consumed) {
        int[][] tConsumed = new int[2][2];
        super.onNestedPreScroll(target, dx, dy, tConsumed[0]);
        dispatchNestedPreScroll(dx, dy, tConsumed[1], null);
        consumed[0] = tConsumed[0][0] + tConsumed[1][0];
        consumed[1] = tConsumed[0][1] + tConsumed[1][1];
    }

    @Override
    public void onNestedScroll(View target, int dxConsumed, int dyConsumed,
                           int dxUnconsumed, int dyUnconsumed) {
        super.onNestedScroll(target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed);
        dispatchNestedScroll(dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed, null);
    }

    @Override
    public boolean onNestedPreFling(View target, float velocityX, float velocityY) {
        boolean tHandled = super.onNestedPreFling(target, velocityX, velocityY);
        return dispatchNestedPreFling(velocityX, velocityY) || tHandled;
    }

    @Override
    public boolean onNestedFling(View target, float velocityX, float velocityY, boolean consumed) {
        boolean tHandled = super.onNestedFling(target, velocityX, velocityY, consumed);
        return dispatchNestedFling(velocityX, velocityY, consumed) || tHandled;
    }

    @Override
    public void setNestedScrollingEnabled(boolean enabled) {
        mChildHelper.setNestedScrollingEnabled(enabled);
    }

    @Override
    public boolean isNestedScrollingEnabled() {
        return mChildHelper.isNestedScrollingEnabled();
    }

    @Override
    public boolean startNestedScroll(int axes) {
        return mChildHelper.startNestedScroll(axes);
    }

    @Override
    public void stopNestedScroll() {
        mChildHelper.stopNestedScroll();
    }

    @Override
    public boolean hasNestedScrollingParent() {
        return mChildHelper.hasNestedScrollingParent();
    }

    @Override
    public boolean dispatchNestedScroll(int dxConsumed, int dyConsumed, int dxUnconsumed,
                                    int dyUnconsumed, int[] offsetInWindow) {
        return mChildHelper.dispatchNestedScroll(dxConsumed, dyConsumed, dxUnconsumed,
                dyUnconsumed, offsetInWindow);
    }

    @Override
    public boolean dispatchNestedPreScroll(int dx, int dy, int[] consumed, int[] offsetInWindow) {
        return mChildHelper.dispatchNestedPreScroll(dx, dy, consumed, offsetInWindow);
    }

    @Override
    public boolean dispatchNestedFling(float velocityX, float velocityY, boolean consumed) {
        return mChildHelper.dispatchNestedFling(velocityX, velocityY, consumed);
    }

    @Override
    public boolean dispatchNestedPreFling(float velocityX, float velocityY) {
        return mChildHelper.dispatchNestedPreFling(velocityX, velocityY);
    }
}


Thx Marc.你的评论肯定让我节省了几个小时.如果有人需要完整的例子 - 会在这里找到它:https://snipsave.com/mateuszpryczkowski/#/snippet/OkfTpFB1f11zdKlH0o
要使其与支持lib v26一起使用,请为onNestedPreScroll,onNestedScroll,onStopNestedScroll和onStartNestedScroll添加覆盖所有类型参数的覆盖.还可以使用这些方法中的类型参数调用帮助程序正确的方法.
这样做了,谢谢.此代码始终将所有滚动操作传递给内部和外部CoordinatorLayout.我必须做的一个微小的改变,只允许内部一个继续消耗滚动,直到滚动到顶部/底部是`onNestedPreScroll()`中的这一行:`dispatchNestedPreScroll(dx - tConsumed [0] [0],dy - tConsumed [0 ] [1],tConsumed [1],null);`.
非常感谢,这应该在支持库中。由于某种原因,该行为与未嵌套的行为仍略有不同,因此我决定像RecyclerView一样从`NestedScrollingChild2`继承。现在,它可以完美运行。这是`NestedCoordinatorLayout`的最终版本:https://gist.github.com/Ghedeon/6f6be99e9960d3c225a4cd7731beee9e
基于Mateusz Pryczkowski gist的带有NestedScrollingChild3的AndroidX NestedCoordinatorLayout,基于https://gist.github.com/ReeMiXeR/97df5d8737380888893d107eccd27bcb

2> dev.bmax..:

这是嵌套协调器布局的简单实现.

/**
 * This variation of CoordinatorLayout also serves as a nested scrolling child,
 * which supports passing nested scrolling operations to it's parent when it's
 * own nested scrolling is locked.
 */
public class NestedCoordinatorLayout extends CoordinatorLayout implements NestedScrollingChild {

    private NestedScrollingChildHelper mChildHelper;
    private volatile boolean mPassToParent;

    public NestedCoordinatorLayout(Context context) {
        super(context);
        mChildHelper = new NestedScrollingChildHelper(this);
        setNestedScrollingEnabled(true);
    }

    public NestedCoordinatorLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        mChildHelper = new NestedScrollingChildHelper(this);
        setNestedScrollingEnabled(true);
    }

    public NestedCoordinatorLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mChildHelper = new NestedScrollingChildHelper(this);
        setNestedScrollingEnabled(true);
    }

    /**
     * Locks the nested scrolling. Further scroll events will
     * be passed to the nested scrolling parent.
     */
    public void lockNestedScrolling() {
        mPassToParent = true;
    }

    /**
     * Unlocks the nested scrolling. Further scroll events will
     * be dispatched to this layout's own scrolling children.
     */
    public void unlockNestedScrolling() {
        mPassToParent = false;
    }

    /*
     * NestedScrollingParent implementation
     */

    @Override
    public boolean onStartNestedScroll(View child, View target, int nestedScrollAxes) {
        /* Enable the scrolling behavior of our own children */
        super.onStartNestedScroll(child, target, nestedScrollAxes);
        /* Enable the scrolling behavior of the parent's other children  */
        startNestedScroll(nestedScrollAxes);
        /* Start tracking the current scroll */
        return true;
    }

    @Override
    public void onStopNestedScroll(View target) {
        /* Disable the scrolling behavior of our own children */
        super.onStopNestedScroll(target);
        /* Disable the scrolling behavior of the parent's other children  */
        stopNestedScroll();
    }

    @Override
    public void onNestedPreScroll(View target, int dx, int dy, int[] consumed) {
        if (mPassToParent) {
            dispatchNestedPreScroll(dx, dy, consumed, null);
        } else {
            super.onNestedPreScroll(target, dx, dy, consumed);
        }
    }

    @Override
    public void onNestedScroll(View target, int dxConsumed, int dyConsumed,
                               int dxUnconsumed, int dyUnconsumed) {
        if (mPassToParent) {
            dispatchNestedScroll(dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed, null);
        } else {
            super.onNestedScroll(target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed);
        }
    }

    @Override
    public boolean onNestedPreFling(View target, float velocityX, float velocityY) {
        if (mPassToParent) {
            return dispatchNestedPreFling(velocityX, velocityY);
        } else {
            return super.onNestedPreFling(target, velocityX, velocityY);
        }
    }

    @Override
    public boolean onNestedFling(View target, float velocityX, float velocityY, boolean consumed) {
        if (mPassToParent) {
            return dispatchNestedFling(velocityX, velocityY, consumed);
        } else {
            return super.onNestedFling(target, velocityX, velocityY, consumed);
        }
    }

    /*
     * NestedScrollingChild implementation
     */

    @Override
    public void setNestedScrollingEnabled(boolean enabled) {
        mChildHelper.setNestedScrollingEnabled(enabled);
    }

    @Override
    public boolean isNestedScrollingEnabled() {
        return mChildHelper.isNestedScrollingEnabled();
    }

    @Override
    public boolean startNestedScroll(int axes) {
        return mChildHelper.startNestedScroll(axes);
    }

    @Override
    public void stopNestedScroll() {
        mChildHelper.stopNestedScroll();
    }

    @Override
    public boolean hasNestedScrollingParent() {
        return mChildHelper.hasNestedScrollingParent();
    }

    @Override
    public boolean dispatchNestedScroll(int dxConsumed, int dyConsumed, int dxUnconsumed,
                                        int dyUnconsumed, int[] offsetInWindow) {
        return mChildHelper.dispatchNestedScroll(dxConsumed, dyConsumed, dxUnconsumed,
                dyUnconsumed, offsetInWindow);
    }

    @Override
    public boolean dispatchNestedPreScroll(int dx, int dy, int[] consumed, int[] offsetInWindow) {
        return mChildHelper.dispatchNestedPreScroll(dx, dy, consumed, offsetInWindow);
    }

    @Override
    public boolean dispatchNestedFling(float velocityX, float velocityY, boolean consumed) {
        return mChildHelper.dispatchNestedFling(velocityX, velocityY, consumed);
    }

    @Override
    public boolean dispatchNestedPreFling(float velocityX, float velocityY) {
        return mChildHelper.dispatchNestedPreFling(velocityX, velocityY);
    }
}



3> natario..:

我将其设置为库,请在GitHub上查看。

您只在布局中使用。

推荐阅读
Gbom2402851125
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有