当布局再次膨胀时,视图为空
Views are null when layouts are inflated again
我有一个包含 4 个容器的视图 (LinearLayouts)。每个容器将根据数据填充内容。 (每个内容视图都用布局膨胀并添加到容器中)
之前我在加载每个布局后调用 ButterKnife.bind(this, view)
加载数据。现在我想摆脱 ButterKnife,转而使用 kotlinx 合成视图绑定。
我的问题是,第一次创建视图并设置一切正常。当我刷新视图时,大多数视图都是空的(之前不是空的,也不应该是空的)。
一些代码:
private fun refresh() {
//Get data...then...
//Reset containers
topup_container_one.removeAllViews()
topup_container_two.removeAllViews()
topup_container_three.removeAllViews()
topup_container_four.removeAllViews()
lateinit var contentOne: View
lateinit var contentTwo: View
lateinit var contentThree: View
lateinit var contentFour: View
val layoutInflater = context!!.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
if (someCondition) {
contentOne = layoutInflater.inflate(R.layout.layout1, topup_container_one, false)
contentTwo = layoutInflater.inflate(R.layout.layout2, topup_container_two, false)
contentThree = layoutInflater.inflate(R.layout.layout3, topup_container_three, false)
contentFour = layoutInflater.inflate(R.layout.layout4, topup_container_four, false)
} else {
// some other sort order, same principle
}
topup_container_one.addView(contentOne)
topup_container_two.addView(contentTwo)
topup_container_three.addView(contentThree)
topup_container_four.addView(contentFour)
}
布局:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:id="@+id/topup_container_one"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
//same for two, three, four
</LinearLayout>
对于视图混乱我能做些什么吗?也许 'recall' 视图绑定?
当我在给每个容器充气后再次手动绑定我的视图时,它起作用了。
那么,我的做法是:
private var buttonVoucherVoiceInput: AppCompatImageButton? = null
private var editTextVoucher: EditText? = null
private var textInputLayoutVoucher: TextInputLayout? = null
private var buttonVoucherSubmit: Button? = null
//And many many more
private fun refresh() {
///...
topup_container_one.addView(contentOne)
topup_container_two.addView(contentTwo)
topup_container_three.addView(contentThree)
topup_container_four.addView(contentFour)
//Find views again
editTextVoucher = view!!.findViewById(R.id.topup_edittext_voucher)
buttonVoucherSubmit = view!!.findViewById(R.id.topup_button_voucher_submit)
textInputLayoutVoucher = view!!.findViewById(R.id.topup_til_voucher)
buttonVoucherVoiceInput = view!!.findViewById(R.id.topup_button_voucher_voice)
}
烦人,但它又能用了。我可以从我的项目中删除 Butterknife。 (没有不尊重,我喜欢它):)
我有一个包含 4 个容器的视图 (LinearLayouts)。每个容器将根据数据填充内容。 (每个内容视图都用布局膨胀并添加到容器中)
之前我在加载每个布局后调用 ButterKnife.bind(this, view)
加载数据。现在我想摆脱 ButterKnife,转而使用 kotlinx 合成视图绑定。
我的问题是,第一次创建视图并设置一切正常。当我刷新视图时,大多数视图都是空的(之前不是空的,也不应该是空的)。
一些代码:
private fun refresh() {
//Get data...then...
//Reset containers
topup_container_one.removeAllViews()
topup_container_two.removeAllViews()
topup_container_three.removeAllViews()
topup_container_four.removeAllViews()
lateinit var contentOne: View
lateinit var contentTwo: View
lateinit var contentThree: View
lateinit var contentFour: View
val layoutInflater = context!!.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
if (someCondition) {
contentOne = layoutInflater.inflate(R.layout.layout1, topup_container_one, false)
contentTwo = layoutInflater.inflate(R.layout.layout2, topup_container_two, false)
contentThree = layoutInflater.inflate(R.layout.layout3, topup_container_three, false)
contentFour = layoutInflater.inflate(R.layout.layout4, topup_container_four, false)
} else {
// some other sort order, same principle
}
topup_container_one.addView(contentOne)
topup_container_two.addView(contentTwo)
topup_container_three.addView(contentThree)
topup_container_four.addView(contentFour)
}
布局:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:id="@+id/topup_container_one"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
//same for two, three, four
</LinearLayout>
对于视图混乱我能做些什么吗?也许 'recall' 视图绑定?
当我在给每个容器充气后再次手动绑定我的视图时,它起作用了。
那么,我的做法是:
private var buttonVoucherVoiceInput: AppCompatImageButton? = null
private var editTextVoucher: EditText? = null
private var textInputLayoutVoucher: TextInputLayout? = null
private var buttonVoucherSubmit: Button? = null
//And many many more
private fun refresh() {
///...
topup_container_one.addView(contentOne)
topup_container_two.addView(contentTwo)
topup_container_three.addView(contentThree)
topup_container_four.addView(contentFour)
//Find views again
editTextVoucher = view!!.findViewById(R.id.topup_edittext_voucher)
buttonVoucherSubmit = view!!.findViewById(R.id.topup_button_voucher_submit)
textInputLayoutVoucher = view!!.findViewById(R.id.topup_til_voucher)
buttonVoucherVoiceInput = view!!.findViewById(R.id.topup_button_voucher_voice)
}
烦人,但它又能用了。我可以从我的项目中删除 Butterknife。 (没有不尊重,我喜欢它):)