查看 Activity 和底部栏的绑定

View Binding Both Activity and Bottom Bar

我正在我的项目中设置视图绑定,但我 运行 遇到了问题。我尝试 look for documentation 关于这个问题,但运气不佳。

我有我的 activity,我已经为此设置了视图绑定:

class AeropressActivity : AppCompatActivity() {
    private lateinit var binding: ActivityAeropressBinding
    private lateinit var bottomSheetBehavior: BottomSheetBehavior<ConstraintLayout>


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityAeropressBinding.inflate(layoutInflater)
        setContentView(binding.root)

        setupBottomSheet()
        onClickListeners()

    }
      private fun setupBottomSheet() {
        bottomSheetBehavior = BottomSheetBehavior.from(findViewById(R.id.layout_bottom_sheet))

        // OnClickListener for bottomSheetBehavior
        bottomSheetBehavior.addBottomSheetCallback(
            object : BottomSheetBehavior.BottomSheetCallback() {
                override fun onSlide(bottomSheet: View, slideOffset: Float) {
                }

                override fun onStateChanged(bottomSheet: View, newState: Int) {
                    
                }
            }
        )
    }

设置完成后,我想为我的 BottomSheet 使用视图绑定,因为它有一些按钮和一个我想添加 onClickListeners 的计时器。

我已经添加了

lateinit var bindingBottomSheet: LayoutBottomSheetBinding

但是膨胀这没有任何作用:

bindingBottomSheet = LayoutBottomSheetBinding.inflate(layoutInflater)

我缺少什么才能让它工作?这可能吗?

编辑:

BottomSheet 布局如下所示:

<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/layout_bottom_sheet"
    android:layout_width="match_parent"
    android:layout_height="140dp"
    app:behavior_peekHeight="50dp"
    tools:context=".BottomSheetActivity"
    app:behavior_hideable="false"
    android:background="@drawable/background_bottom_bar"
    app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">


    <ImageView
        android:id="@+id/image_view_button_home"
        android:src="@drawable/ic_home"
        android:layout_width="40dp"
        android:layout_height="35dp"
        android:layout_marginStart="24dp"
        android:layout_marginTop="10dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageView
        android:id="@+id/image_view_button_timer"
        android:layout_width="40dp"
        android:layout_height="35dp"
        android:layout_marginTop="10dp"
        android:src="@drawable/ic_stopwatch"
        app:layout_constraintLeft_toRightOf="@id/image_view_button_home"
        app:layout_constraintRight_toLeftOf="@id/image_view_button_info"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageView
        android:src="@drawable/ic_info"
        android:id="@+id/image_view_button_info"
        android:layout_width="40dp"
        android:layout_height="35dp"
        android:layout_marginTop="10dp"
        android:layout_marginEnd="24dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintLeft_toRightOf="@id/image_view_button_timer"
        app:layout_constraintTop_toTopOf="parent" />


    <Button
        android:id="@+id/button_bottom_reset"
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:text="Reset"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/image_view_button_info" />

    <Chronometer
        android:gravity="center_horizontal"
        android:textSize="48sp"
        android:textColor="@color/white"
        android:format="%s"
        android:id="@+id/chronometer_bottom_bar"
        android:layout_width="0dp"
        android:paddingStart="6dp"
        android:paddingEnd="6dp"
        android:layout_height="60dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@id/button_bottom_start"
        app:layout_constraintStart_toEndOf="@id/button_bottom_reset"
        app:layout_constraintTop_toBottomOf="@id/image_view_button_timer" />

    <Button
        android:id="@+id/button_bottom_start"
        android:layout_width="100dp"
        android:layout_height="40dp"
        android:text="Start"
        app:layout_constraintBottom_toTopOf="@id/button_bottom_stop"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@id/image_view_button_home" />
    <Button
        android:id="@+id/button_bottom_stop"
        android:layout_width="100dp"
        android:layout_height="40dp"
        android:text="Stop"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@id/button_bottom_start" />


</androidx.constraintlayout.widget.ConstraintLayout>

The BottomSheet has its own layout which is added to the activity's layout by using the .

I want to use View Binding for my BottomSheet, as it has some buttons and a Chronometer that I want to add onClickListeners to.

因此,您无法从 AeropressActivity

访问 BottomSheet 的底层视图
  • 首先确保 BottomSheet 布局包含在 <layout></layout> 标签中。

  • 然后确保 <include> 有一个 ID,以便它允许您使用数据绑定访问 BottomSheet 布局。

AeropressActivity布局:

<layout>
    .
    .
    <include
        android:id="@+id/bottom_sheet"
        layout="@layout/bottom_sheet" />

</layout>

然后访问 BottomSheet 布局中的按钮:

bindingBottomSheet.bottomSheet.myButtonId

假设按钮id为:my_button_id