我正在回收者视图中显示项目列表。 item_layout.xml具有一个include,其父元素为ContraintLayout,并且其中包含一些元素,我可以通过view.elementID
轻松地指向它们。<androidx.constraintlayout.widget.ConstraintLayout...>
...
<include
android:id="@+id/incNumberPicker"
layout="@layout/number_picker"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/padding_big"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/lblFamily"
app:layout_constraintWidth_percent="0.5" />
...
</androidx.constraintlayout.widget.ConstraintLayout>
我可以获取包含布局的引用,但不能获取此包含的父布局的引用。 我尝试过...
private val incNumberPicker = view.findViewById<ConstraintLayout>(R.id.incNumberPicker) (not null)
private val bgNumberPicker = view.bgNumberPicker (null)
private val bgNumberPicker = incNumberPicker.findViewById<ConstraintLayout>(R.id.bgNumberPicker) (null)
private val bgNumberPicker = view.findViewById<ConstraintLayout>(R.id.bgNumberPicker)(null)
这是我的收录布局
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/bgNumberPicker"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:focusableInTouchMode="true"
android:layout_height="wrap_content"
android:background="@drawable/number_picker_bg">
<EditText
android:id="@+id/etNumber"
style="@style/et_number_picker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/padding_normal"
android:layout_marginEnd="@dimen/padding_normal"
android:backgroundTint="@android:color/transparent"
android:inputType="number"
android:maxLength="3"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/btnAdd"
app:layout_constraintStart_toEndOf="@+id/btnRemove"
app:layout_constraintTop_toTopOf="parent" />
此父约束有一个背景,我想根据状态更改颜色,因此我想启用/禁用此约束布局。问题是我可以使用普通view.etNumber轻松地指向其余元素,但不能指向父约束。
更新跟踪错误
E/AndroidRuntime: FATAL EXCEPTION: main
Process: dev.game.jupiter, PID: 29108
java.lang.IllegalStateException: bgNumberPicker must not be null
at es.game.jupiter.ui.inventory.productsCounter.InventoriedProductAdapter$InventoriedProductViewHolder.setLocationPickerEditable(InventoriedProductAdapter.kt:95)
at es.game.jupiter.ui.inventory.productsCounter.InventoriedProductAdapter$InventoriedProductViewHolder.hideShowOptions(InventoriedProductAdapter.kt:85)
at es.game.jupiter.ui.inventory.productsCounter.InventoriedProductAdapter$InventoriedProductViewHolder$3.onClick(InventoriedProductAdapter.kt:65)
at android.view.View.performClick(View.java:5205)
感谢您的帮助。