日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

Android自定义Behavior

發布時間:2023/12/20 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android自定义Behavior 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

先上效果圖:

Behavior是CoordinatorLayout的一個泛型抽象內部類,所以給子view添加layout_behavior屬性是來自于它。

實現過程:

在CoordinatorLayout父控件中,滾動NestedScrollView,使AppBarLayout也隨之顯示隱藏,通過我們的自定義behavior,監聽AppBarLayout的位置,來對底部菜單欄的位置高度作相應的改變。

github代碼直通車

布局文件代碼:

?

<?xml version="1.0" encoding="utf-8"?> <androidx.coordinatorlayout.widget.CoordinatorLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"xmlns:app="http://schemas.android.com/apk/res-auto"app:layout_constraintTop_toTopOf="parent"app:layout_constraintBottom_toTopOf="@id/llMenu"xmlns:android="http://schemas.android.com/apk/res/android"><com.google.android.material.appbar.AppBarLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"><androidx.appcompat.widget.Toolbarandroid:layout_width="match_parent"android:layout_height="wrap_content"app:layout_scrollFlags="scroll|enterAlways"></androidx.appcompat.widget.Toolbar></com.google.android.material.appbar.AppBarLayout><androidx.core.widget.NestedScrollViewandroid:layout_width="match_parent"android:layout_height="match_parent"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><ImageViewandroid:layout_width="match_parent"android:layout_height="200dp"android:scaleType="centerCrop"android:background="@mipmap/pic1"/><ImageViewandroid:layout_width="match_parent"android:layout_height="200dp"android:scaleType="centerCrop"android:background="@mipmap/pic2"/><ImageViewandroid:layout_width="match_parent"android:layout_height="200dp"android:scaleType="centerCrop"android:background="@mipmap/pic1"/><ImageViewandroid:layout_width="match_parent"android:layout_height="200dp"android:scaleType="centerCrop"android:background="@mipmap/pic2"/><ImageViewandroid:layout_width="match_parent"android:layout_height="200dp"android:scaleType="centerCrop"android:background="@mipmap/pic1"/><ImageViewandroid:layout_width="match_parent"android:layout_height="200dp"android:scaleType="centerCrop"android:background="@mipmap/pic2"/></LinearLayout></androidx.core.widget.NestedScrollView><LinearLayoutandroid:id="@+id/llMenu"android:layout_width="match_parent"android:layout_height="48dp"android:layout_gravity="bottom"android:background="#fff"app:layout_constraintBottom_toBottomOf="parent"android:orientation="horizontal"app:layout_behavior=".MyBehavior"><TextViewandroid:layout_width="0dp"android:layout_weight="1"android:layout_height="match_parent"android:gravity="center"android:textSize="16sp"android:text="首頁"/><TextViewandroid:layout_width="0dp"android:layout_weight="1"android:layout_height="match_parent"android:gravity="center"android:textSize="16sp"android:text="推薦"/><TextViewandroid:layout_width="0dp"android:layout_weight="1"android:layout_height="match_parent"android:gravity="center"android:textSize="16sp"android:text="我的"/></LinearLayout> </androidx.coordinatorlayout.widget.CoordinatorLayout>

自定義behavior:

?

public class MyBehavior extends CoordinatorLayout.Behavior<View> {public MyBehavior(Context context, AttributeSet attrs) {super(context, attrs);}@Overridepublic boolean layoutDependsOn(@NonNull CoordinatorLayout parent, @NonNull View child, @NonNull View dependency) {return dependency instanceof AppBarLayout; //讓AppBarLayout為dependency}@Overridepublic boolean onDependentViewChanged(@NonNull CoordinatorLayout parent, @NonNull View child, @NonNull View dependency) {//獲取依據的view的高度百分比float heightRatio = dependency.getY()/dependency.getHeight();child.setTranslationY(-child.getHeight()*heightRatio);return true;} }

這里一定要寫上帶參數的構造方法,因為coordinatorlayout是根據反射(所以是包名.類名路徑)獲取這個behavior。

總結

以上是生活随笔為你收集整理的Android自定义Behavior的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。