如何在 android 中使用 kotlin 合成从包含的布局访问视图

时间:2021-04-08 08:02:18

标签: android kotlin android-viewbinding

在我的片段布局中,我添加了一个包含的布局

<?xml version="1.0" encoding="utf-8"?>
<layout 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">

<data>

  
</data>

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:itemCount="3"
        tools:listitem="@layout/list_item_eceived" />

    <include
        layout="@layout/item_user_input"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

我包含的布局

<?xml version="1.0" encoding="utf-8"?>
<layout 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">

<data>

    <variable
        name="hint"
        type="String" />
    <variable
        name="available"
        type="Boolean" />
</data>

<androidx.constraintlayout.widget.ConstraintLayout
    android:id="@+id/message_box_parent"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="12dp"
    android:elevation="2dp"
    android:minHeight="50dp"
    app:layout_goneMarginStart="6dp">

    <com.google.android.material.textfield.TextInputEditText
        android:id="@+id/message_view"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="4dp"
        android:focusable="true"
        android:hint="@{hint}"
        android:maxHeight="128dp"
        android:padding="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/send_btn_view"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <androidx.appcompat.widget.AppCompatImageView
        android:id="@+id/send_btn_view"
        android:layout_width="44dp"
        android:layout_height="44dp"
        android:layout_marginEnd="2dp"
        android:paddingStart="12dp"
        android:paddingTop="8dp"
        android:paddingEnd="8dp"
        android:paddingBottom="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>

我在片段中访问视图

send_btn_view.setOnClickListener { 
        Log.e("button","clicked")
    }

综合导入是 import kotlinx.android.synthetic.main.item_user_input.*

当我运行应用程序时出现错误

Unresolved reference: item_user_input

1 个答案:

答案 0 :(得分:0)

不能,因为如果多次包含相同的布局,Synthetic 库将不知道该怎么做。

无论如何,Kotlin Synthetic 已弃用,Google 官方建议迁移到视图/数据绑定: https://developer.android.com/topic/libraries/view-binding/migration

由于您已经使用了数据绑定,我建议您使用绑定类来访问视图。这样你就明确提到了包含的布局。

首先,在包含的布局中添加一个 id:

   <include
        android:id="@+id/item_user_input"
        layout="@layout/item_user_input"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

二、访问视图:

binding.itemUserInput.sendBtnView