[英]How listen change height bottom sheet layout android
我有以下代碼
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".ui.main.MainActivity">
<fragment
android:id="@+id/navHostFragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="@navigation/main_graph" />
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:id="@+id/coordinatorLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="8dp">
<include layout="@layout/bottom_sheet" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
和
<?xml version="1.0" encoding="utf-8"?><!--suppress AndroidUnknownAttribute -->
<androidx.constraintlayout.widget.ConstraintLayout 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:id="@+id/bottomSheet"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/bottom_sheet_corners"
android:clickable="true"
android:focusable="true"
app:behavior_hideable="false"
app:behavior_peekHeight="0dp"
app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior"
tools:ignore="ContentDescription">
<com.mobilemedia.tanuki.ui.view.RoundedView
android:id="@+id/bottomSheetContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:topLeftCornerRadius="16dp"
app:topRightCornerRadius="16dp">
<fragment
android:id="@+id/bottomSheetNavHostFragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="@navigation/bottom_sheet_graph" />
</com.mobilemedia.tanuki.ui.view.RoundedView>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/collapsedView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<View
android:layout_width="32dp"
android:layout_height="4dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:background="@drawable/bottom_sheet_indicator"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
我需要聆聽底部表格布局的高度變化,以更改相對於該高度的元素的 position
盡管該項目位於 navHostFragment 中的另一個片段上,但該項目必須始終位於距底部工作表的高度
如何從底部聽改變高度?
通常情況下,您可以使用BottomSheetCallback收聽底部幻燈片。 覆蓋 onSlide 方法並將您的操作放入其中。 此方法在底部工作表被拖動時調用。 並且可以獲得bottomSheet的所有屬性,包括高度。
但更好的方法是實現自定義 CoordinatorLayout.Behavior。 僅當您的依賴視圖和 BottomSheet 是同一 coordinatorlayout 的子級時,這才有效。 只需擴展CoordinatorLayout.Behavior並覆蓋layoutDependsOn和onDependentViewChanged方法。 CoordinatorLayout 就是為了解決這樣的問題。 互聯網上有很多關於此的文章, 例如。 android 庫中還有一個標准的浮動操作按鈕行為。
第三種方式是使用 MotionLayout。 但它更復雜,而且可能很難開始。 所以我不應該建議它,因為它對於這項任務可能過於強大。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.