CorodinatorLayout
在另一个内部CoordinatorLayout
,滚动子视图也应该滚动父CoordinatorLayout
.
我有一个coordinatorLayout
与ViewPager
包含不同Fragment
,使得在Scroll
将隐藏tabLayout
我有另一个coordinatorLayout
有viewPager
.这fragment
是ViewPager
父片段(父母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)将被隐藏.
我知道这是一个老问题.但是我搜索了很长时间才将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); } }
这是嵌套协调器布局的简单实现.
/** * 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); } }
我将其设置为库,请在GitHub上查看。
您只
在布局中使用。