android - 内部布局包括适配器内部始终为空
问题描述
我正在回收站视图中显示项目列表。item_layout.xml 有一个包含,它的父级是一个 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)
谢谢你的帮助。
解决方案
推荐阅读
- regex - How to match my whole string with regular expression while allowing nested patterns
- php - 在注册表 Prestashop 1.6.x 中显示自定义字段
- python - 如何将具有不同索引的熊猫中的两列相乘?
- c# - 谷歌云服务可以与 Unity 一起使用吗?
- django - How to load the data into existing table in postgresql database in django
- visual-studio-code - 让 VSCode Intellisense 默认建议“if block”而不是“if”
- php - 如何将 SQL Server 与 PHP 7.3 连接起来?
- android - ConstraintLayout: EditText.requestFocus() is not working normally when it is added in Group
- powershell - 选择格式已损坏
- c# - 在 64 位中禁用 QuickEditMode