如何解决视图绑定活动和底部栏
我正在我的项目中设置视图绑定,但遇到了一个问题。我试图look for documentation在这件事下不走运。
我有我的活动,我已经为此设置了视图绑定:
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>
解决方法
BottomSheet 有自己的布局,使用 .
我想为我的BottomSheet 使用视图绑定,因为它有一些按钮和一个我想添加onClickListeners 的计时器。
因此,您无法从 AeropressActivity
-
首先确保
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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。