Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/231.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何对嵌套类使用行为?_Android_Android Coordinatorlayout - Fatal编程技术网

Android 如何对嵌套类使用行为?

Android 如何对嵌套类使用行为?,android,android-coordinatorlayout,Android,Android Coordinatorlayout,当AppToolBar隐藏时,我正在进行隐藏视图的操作。如果我将带有行为的Cardview放在定义了CoordinatorLayout的同一xml中,这个方法就有效了。然而,我需要更深层的层次结构,当我在片段中使用它时,它不再起作用 public class BottomBehaviour extends CoordinatorLayout.Behavior<CardView> { int totalHeigh; float dencity; int statusBar

当AppToolBar隐藏时,我正在进行隐藏视图的操作。如果我将带有行为的Cardview放在定义了CoordinatorLayout的同一xml中,这个方法就有效了。然而,我需要更深层的层次结构,当我在片段中使用它时,它不再起作用

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);
  }
}
public类行为扩展了CoordinatorLayout.Behavior{
整数总高度;
浮动密集度;
内特状态巴海;
公众行为{
}
公共行为(上下文、属性集属性){
超级(上下文,attrs);
dencity=context.getResources().getDisplayMetrics().density;
int height=context.getResources().getDisplayMetrics().heightPixels;
statusBarHeigh=GetStatusBarHeigh(上下文);
总高度=高度;
//totalHeigh=(int)(context.getResources().getDisplayMetrics().heightPixels-(56*dencity));
Log.e(“mcheck”,“bottombehavior:”);
}
public int getStatusBarHeight(上下文){
int结果=0;
int resourceId=context.getResources().getIdentifier(“status\u bar\u height”、“dimen”、“android”);
如果(资源ID>0){
结果=context.getResources().getDimensionPixelSize(resourceId);
}
返回结果;
}
@凌驾
公共布尔布局依赖项(协调布局父项、CardView子项、视图依赖项){
返回AppBarLayout的依赖实例;
}
@凌驾
公共布尔onDependentViewChanged(协调布局父项、CardView子项、视图依赖项){
inttop=(int)(totalHeigh child.getHeight()-(dependency.getY());
child.setTranslationY(顶部);
Log.e(“mcheck”、“onDependentViewChanged:”+(dependency.getY()-statusBarHeigh)+“height”+dependency.getHeight()/dencity);
返回super.onDependentViewChanged(父、子、依赖项);
}
}
一些片段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
s仅当目标视图,即包含
app:layout\u Behavior=“ua.miui.forum.widget.bottomBehavior”
的视图是
CoordinatorLayout
的直接后代时才起作用。否则,它将不会接收嵌套的滚动事件

如果您的
LinearLayout
CoordinatorLayout
的直接子级,请将行为添加到它,然后您可能应该调整您的
cardwiew
查看视图id,并对嵌套滚动进行操作