一、layout_behavior是什麼?
1、layout_behavior是Android自定義Behavior的一種,可在布局文件中通過app:layout_behavior屬性設置。這意味着,layout_behavior指定了對應布局的交互方式和效果,如使某個view滑動隱藏頭部導航欄。
2、默認情況下,每個view或viewGroup的布局都具有一種默認的behavior,可通過給view或viewGroup設置app:layout_behavior屬性切換behavior的類型,但只能有一個behavior與view或viewGroup相關聯。
3、layout_behavior需實現CoordinatorLayout.Behavior接口,即可具備behavior特性,以與CoordinatorLayout協調調用。
二、CoordinatorLayout的布局機制
1、CoordinatorLayout是一個主要用於AppBarLayout和FloatingActionButton之類的可交互View之間布局和動畫的容器。
2、CoordinatorLayout通過分析以下三種事件:
CoordinatorLayout嵌套子View的大小位置變化、子View的觸摸事件、子View的滾動事件。
來確定子View之間的位置關係。
3、CoordinatorLayout在內置的Behavior中集成了所有子View的布局邏輯,可以通過自定義Behavior來擴展CoordinatorLayout的功能來進行視圖之間的關聯。
三、如何自定義Behavior
1、創建一個Java類並實現CoordinatorLayout.Behavior接口,然後重寫Behavior的三個方法:
public boolean layoutDependsOn(CoordinatorLayout parent, V child, View dependency) public boolean onDependentViewChanged(CoordinatorLayout parent, V child, View dependency) public boolean onLayoutChild(CoordinatorLayout parent, V child, int layoutDirection)
2、使用@CoordinatorLayout.DefaultBehavior註解對Behavior進行標記,指定用於該view的默認Behavior。
3、自定義Behavior的實現主要是應用其依賴關係來確定相對布局,以及在其依賴項發生變化時對其相應進行動態修改處理。
四、layout_behavior的使用
1、在布局文件中使用定義的Behavior
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/linearlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior=".MyBehavior">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
2、在自定義Behavior中定義與其關聯的view和dependency
public class MyBehavior extends CoordinatorLayout.Behavior<LinearLayout> {
public MyBehavior(Context context, AttributeSet attrs) {
super(context, attrs);
}
public boolean layoutDependsOn(CoordinatorLayout parent, LinearLayout child, View dependency) {
//依賴於AppBarLayout
return dependency instanceof AppBarLayout;
}
public boolean onDependentViewChanged(CoordinatorLayout parent, LinearLayout child, View dependency) {
return false;
}
public boolean onLayoutChild(CoordinatorLayout parent, LinearLayout child, int layoutDirection) {
return false;
}
}
五、layout_behavior的應用案例
1、實現滑動隱藏頭部導航欄的效果
public class MyAppBarLayoutBehavior extends AppBarLayout.Behavior {
public MyAppBarLayoutBehavior() {
super();
}
public MyCoordinatorLayoutBehavior(Context context, AttributeSet attrs) {
super(context, attrs);
}
public boolean onStartNestedScroll(CoordinatorLayout parent, AppBarLayout child, View directTargetChild, View target, int nestedScrollAxes) {
return nestedScrollAxes == SCROLL_AXIS_VERTICAL;
}
public void onNestedScroll(CoordinatorLayout coordinatorLayout, AppBarLayout child, View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed) {
if (dyConsumed > 0 && child.getVisibility() == View.VISIBLE) {
child.animate().alpha(0.0f).scaleX(0.0f).scaleY(0.0f).setDuration(200).setListener(new AnimatorListenerAdapter() {
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
child.setVisibility(View.GONE);
}
});
} else if (dyConsumed < 0 && child.getVisibility() != View.VISIBLE) {
child.setVisibility(View.VISIBLE);
child.animate().alpha(1.0f).scaleX(1.0f).scaleY(1.0f).setDuration(200);
}
}
}
2、實現浮動按鈕滑動隱藏的效果
public class MyFloatingActionButtonBehavior extends FloatingActionButton.Behavior {
public MyFloatingActionButtonBehavior() {
super();
}
public MyFloatingActionButtonBehavior(Context context, AttributeSet attrs) {
super(context, attrs);
}
public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, FloatingActionButton child, View directTargetChild, View target, int axes) {
return axes == ViewCompat.SCROLL_AXIS_VERTICAL || super.onStartNestedScroll(coordinatorLayout, child, directTargetChild, target, axes);
}
public void onNestedScroll(CoordinatorLayout coordinatorLayout, FloatingActionButton child, View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed) {
if (dyConsumed >= 0) {
animateOut(child);
} else {
animateIn(child);
}
}
private void animateOut(FloatingActionButton button) {
CoordinatorLayout.LayoutParams layoutParams = (CoordinatorLayout.LayoutParams) button.getLayoutParams();
int bottomMargin = layoutParams.bottomMargin;
button.animate().translationY(button.getHeight()+bottomMargin).setInterpolator(new AccelerateInterpolator(3)).start();
}
private void animateIn(FloatingActionButton button) {
button.animate().translationY(0).setInterpolator(new DecelerateInterpolator(3)).start();
}
}
六、總結
本文從layout_behavior的定義、CoordinatorLayout的布局機制、自定義Behavior的實現、layout_behavior的使用方法和案例分別進行了詳細的闡述,幫助讀者更加深入地了解layout_behavior在Android中的作用和細節,為其在實際開發中的應用提供了參考。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/252810.html
微信掃一掃
支付寶掃一掃