RecyclerView 和 CoordinatorLayout.Behavior
RecyclerView and CoordinatorLayout.Behavior
我想了解 Coordinator Custom 行为的工作原理,所以我做了一个项目,当 recyclerview 滚动时,按钮 Y 比例会发生变化。
这是我的 xml 布局:
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/coordinatorLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/mp_recycleview1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/cardview_light_background"
android:paddingBottom="5dp"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:paddingTop="5dp"
android:scrollbars="vertical" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/cardview_dark_background"
android:backgroundTint="@color/colorPrimary"
android:text="Button"
app:layout_behavior=".MyBehavior"
android:textColor="@android:color/white"
/>
</android.support.design.widget.CoordinatorLayout>
这是我的行为class
public class MyBehavior extends CoordinatorLayout.Behavior<Button> {
private final static String TAG = "MyBehavior";
public MyBehavior(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean layoutDependsOn(CoordinatorLayout parent, Button child, View dependency) {
return dependency instanceof RecyclerView;
}
@Override
public void onNestedScroll(CoordinatorLayout coordinatorLayout, Button child, View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed) {
child.setScaleY(((RecyclerView)target).computeVerticalScrollOffset());
}
}
但是当我滚动 RecyclerView 时没有任何反应。
注意这里,
为了接收 onNestedScroll()
呼叫,您需要将 onStartNestedScroll
覆盖为 return true
.
我想了解 Coordinator Custom 行为的工作原理,所以我做了一个项目,当 recyclerview 滚动时,按钮 Y 比例会发生变化。 这是我的 xml 布局:
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/coordinatorLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/mp_recycleview1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/cardview_light_background"
android:paddingBottom="5dp"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:paddingTop="5dp"
android:scrollbars="vertical" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/cardview_dark_background"
android:backgroundTint="@color/colorPrimary"
android:text="Button"
app:layout_behavior=".MyBehavior"
android:textColor="@android:color/white"
/>
</android.support.design.widget.CoordinatorLayout>
这是我的行为class
public class MyBehavior extends CoordinatorLayout.Behavior<Button> {
private final static String TAG = "MyBehavior";
public MyBehavior(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean layoutDependsOn(CoordinatorLayout parent, Button child, View dependency) {
return dependency instanceof RecyclerView;
}
@Override
public void onNestedScroll(CoordinatorLayout coordinatorLayout, Button child, View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed) {
child.setScaleY(((RecyclerView)target).computeVerticalScrollOffset());
}
}
但是当我滚动 RecyclerView 时没有任何反应。
注意这里,
为了接收 onNestedScroll()
呼叫,您需要将 onStartNestedScroll
覆盖为 return true
.