簡體   English   中英

如何使用 android 合成的 kotlin 從包含的布局訪問視圖

[英]How to access view from included layout with kotlin synthetic in android

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

<?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

你不能,因為如果同一個布局被多次包含,合成庫將不知道該怎么做。

無論如何,Kotlin Synthetic 已被棄用,Google 官方建議遷移到視圖/數據綁定: https://developer.android.com/topic/libraries/view/data binding

由於您已經使用數據綁定,我建議使用綁定 class 來訪問視圖。 這樣您就可以明確提及包含的布局。

首先,在包含的布局中添加一個 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM