2017-01-24 9 views
0

AppToolBarが非表示になったときに表示を非表示にするように動作させています。これは、私がCoordinatorLayoutが定義されている同じxmlで動作するようにCardviewを配置すると機能します。しかし、私はそれがより深い階層で動作することを必要とし、私はフラグメントでそれを使用しても、それ以上は動作しません。ネストしたクラスの振る舞いを使用するには?

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

CoordinatorLayout.Behavior作品のみターゲット表示する場合、app:layout_behavior="ua.miui.forum.widget.BottomBehaviour"を保持即ち図でCoordinatorLayoutの直接の子孫です。そうしないと、ネストされたスクロールイベントは受信されません。

あなたLinearLayoutがそれに動作を追加、CoordinatorLayoutの直接の子であり、その後、あなたはおそらくネストされたスクロールであなたのCardViewと行為のために(例えばビューidのために)見て、あなたを微調整する必要がある場合。

関連する問題