问题描述
CorodinatorLayout
在另一个CoordinatorLayout
中,这样滚动子视图也应该滚动父级CoordinatorLayout
.
CorodinatorLayout
inside another CoordinatorLayout
such that scrolling the child-view should also scroll the Parent CoordinatorLayout
.
我有一个coordinatorLayout
,其中ViewPager
包含不同的Fragment
,因此在Scroll
上将隐藏tabLayout
I have a coordinatorLayout
with ViewPager
that contains different Fragment
such that on Scroll
will hide the tabLayout
我有另一个具有viewPager
的coordinatorLayout
.此fragment
在父片段(父Coordinator layout
)的ViewPager
中膨胀.
I have another coordinatorLayout
that has a viewPager
. This fragment
is inflated in ViewPager
of parent fragment(parent Coordinator layout
).
问题是onScrolling
childViewpager
中的子片段仅反映在子fragment
的coordinator layout
中,而不反映在我需要隐藏tablayout
的父coordinator layout
中.
The problem is onScrolling
the child fragment in childViewpager
only reflects in coordinator layout
of child fragment
and not in the Parent coordinator layout
that I need to do to hide the tablayout
.
结构为:
CoordinatorLayout(p) ->(tablayout(p) & ViewPager(p) -> CoordinatorLayout(c) ->(tablayout(c) & ViewPAger(c) ->recyclerView(cc)))
p -> parent;
c -> child; cc -> child to child
如何在滚动回收站视图上进行操作会影响协调器布局,因此工具栏选项卡布局(p)将被隐藏.
How to make on scrolling recycler view will affect both coordinator layout so that the toolbar tablayout(p) will be get hides.
推荐答案
我知道这是一个老问题.但是我搜索了很长时间,将一个片段中的CoordinatorLayout
包含在另一个CoordinatorLayout
中.
I know it's an old question.But I searched a long time to include an CoordinatorLayout
in a fragment, which is in another CoordinatorLayout
.
我稍微修改了dev.bmax的答案,以调用两个协调器布局并调用这两个布局的附加行为.
I modified the answer of dev.bmax a little bit to call both coordinator layouts and call the attached behaviors of both layouts.
这是我的解决方法.
@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);
}
}
这篇关于另一个CoordinatorLayout中的CoordinatorLayout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!