当AppToolBar隐藏时,我正在隐藏视图。如果我将Cardview的行为放在定义了CoordinatorLayout的相同xml中,则可以使用此方法。但是我需要更深层次的层次结构,当我在Fragments中使用它时它不再起作用了。
public class BottomBehaviour extends CoordinatorLayout.Behavior<CardView> {
int totalHeigh;
float dencity;
int statusBarHeigh;
public BottomBehaviour() {
}
public BottomBehaviour(Context context, AttributeSet attrs) {
super(context, attrs);
dencity=context.getResources().getDisplayMetrics().density;
int height = context.getResources().getDisplayMetrics().heightPixels;
statusBarHeigh = getStatusBarHeight(context);
totalHeigh=height;
//totalHeigh = (int) (context.getResources().getDisplayMetrics().heightPixels - (56 * dencity));
Log.e("mcheck", "BottomBehaviour: ");
}
public int getStatusBarHeight(Context context) {
int result = 0;
int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
result = context.getResources().getDimensionPixelSize(resourceId);
}
return result;
}
@Override
public boolean layoutDependsOn(CoordinatorLayout parent, CardView child, View dependency) {
return dependency instanceof AppBarLayout;
}
@Override
public boolean onDependentViewChanged(CoordinatorLayout parent, CardView child, View dependency) {
int top = (int)(totalHeigh-child.getHeight()-(dependency.getY()));
child.setTranslationY(top);
Log.e("mcheck", "onDependentViewChanged: "+(dependency.getY()-statusBarHeigh)+" height "+dependency.getHeight()/dencity);
return super.onDependentViewChanged(parent, child, dependency);
}
}
一些片段xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/tools"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1" />
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="56dip"
app:layout_behavior="ua.miui.forum.widget.BottomBehaviour"
card_view:cardCornerRadius="0dip"
card_view:cardElevation="8dip">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
答案 0 :(得分:0)
CoordinatorLayout.Behavior
的视图)是app:layout_behavior="ua.miui.forum.widget.BottomBehaviour"
的直接后代时, CoordinatorLayout
才有效。否则它将不会收到嵌套的滚动事件。
如果您的LinearLayout
是CoordinatorLayout
的直接子项,请将行为添加到其中,然后您应该调整一下以查找(例如,查看内容ID)您的CardView
并采取行动在嵌套滚动。