如何对嵌套 类 使用行为?
How to use Behavior for nested classes?
我正在执行在 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>
CoordinatorLayout.Behavior
仅在目标视图(即包含 app:layout_behavior="ua.miui.forum.widget.BottomBehaviour"
的视图是 CoordinatorLayout
的直接后代时才有效。否则它不会收到嵌套的滚动事件。
如果您的 LinearLayout
是 CoordinatorLayout
的直接子代,请向其添加行为,然后您可能应该调整您以查找(例如视图 ID)您的CardView
并作用于嵌套滚动。
我正在执行在 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>
CoordinatorLayout.Behavior
仅在目标视图(即包含 app:layout_behavior="ua.miui.forum.widget.BottomBehaviour"
的视图是 CoordinatorLayout
的直接后代时才有效。否则它不会收到嵌套的滚动事件。
如果您的 LinearLayout
是 CoordinatorLayout
的直接子代,请向其添加行为,然后您可能应该调整您以查找(例如视图 ID)您的CardView
并作用于嵌套滚动。