2016-10-21 56 views
1

私は、レイアウトの上部にImageView、その下にRecyclerViewを持っています。それは私のカスタムビヘイビアステップ1で必要なものです。しかし、それはうまくいくようには見えません。 XMLで
Behavior.java私のカスタムビヘイビアがAndroidでうまくいかない理由

public class Depency extends CoordinatorLayout.Behavior<RecyclerView> { 
    private String TAG = "tag"; 

    public Depency() { 
    } 

    public Depency(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    } 

    @Override 
    public boolean onDependentViewChanged(CoordinatorLayout parent, RecyclerView child, View 
     dependency) { 
    int delta = (int) (dependency.getTranslationY() + dependency.getBottom()); 
    delta -= child.getTop(); 
    child.offsetTopAndBottom(delta); 
    Log.i(TAG, 
      "onDependentViewChanged: " + delta + "," + child.getTop() + dependency.getClass().getSimpleName()); 
    return true; 
    } 

    @Override 
    public boolean layoutDependsOn(CoordinatorLayout parent, RecyclerView child, View dependency) { 
    return dependency instanceof AppCompatImageView; 
    } 
} 

layout.xml

<android.support.design.widget.CoordinatorLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" 
    tools:context="com.example.kenchan.fullypjo.MainActivity" 
    tools:showIn="@layout/activity_main"> 


    <android.support.v7.widget.AppCompatImageView 
    android:id="@+id/imageView" 
    android:layout_width="match_parent" 
    android:layout_height="150dp" 
    android:scaleType="centerCrop" 
    android:src="@drawable/ic_girl"/> 

    <android.support.v7.widget.RecyclerView 
    android:id="@+id/recyclerView" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    app:layout_behavior="com.example.kenchan.fullypjo.view.Depency"/> 


</android.support.design.widget.CoordinatorLayout> 

プレビュー:ここ

はコードである
enter image description here
は正常に動作ようです。

しかし、私は私の携帯電話上でプロジェクトを実行すると、私は間違って取得:

enter image description here

誰かが私を助けたり、私のコードを私に助言してもらえますか?
問題はしばらく私を悩ませました。

私はandroid.support.design.widget.AppBarLayout$ScrollingViewBehavior にソースコードを読むことを試みたと私は違う何でコードのロジックが見つかりました:

@Override 
public boolean onDependentViewChanged(CoordinatorLayout parent, View child, 
      View dependency) { 
    offsetChildAsNeeded(parent, child, dependency); 
    return false; 
} 

private void offsetChildAsNeeded(CoordinatorLayout parent, View child, View dependency) { 
    final CoordinatorLayout.Behavior behavior = 
       ((CoordinatorLayout.LayoutParams) dependency.getLayoutParams()).getBehavior(); 
    if (behavior instanceof Behavior) { 
     // Offset the child, pinning it to the bottom the header-dependency, maintaining 
    // any vertical gap and overlap 
    final Behavior ablBehavior = (Behavior) behavior; 
    ViewCompat.offsetTopAndBottom(child, (dependency.getBottom() - child.getTop()) 
        + ablBehavior.mOffsetDelta 
        + getVerticalLayoutGap() 
        - getOverlapPixelsForOffset(dependency)); 
    } 
} 

答えて

0

RecyclerViewImageViewの上にオーバーレイとして、あなたがそれを見ている理由です、トップZインデックスであります。プレビューでは9個のアイテムしか表示されません。

あなたの行動のonLayoutChild()メソッド内でRecyclerViewを変更する場合は、ImageViewの末尾に変更する必要があります。この方法では、CoordinatorLayoutに意図的に行うのではなく、子供を自分でレイアウトする機会が与えられます。 trueを返すと、CoordinatorLayoutはそのビューのレイアウトを試みません。

関連する問題