Представления являются нулевыми, когда макеты снова раздуваются
У меня есть вид с 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>
Что я могу сделать с этим беспорядком? Может быть, "вспомнить" вид привязки?
1 ответ
Когда я снова связываю свои виды вручную после раздувания каждого контейнера, это работает.
Итак, что я делаю:
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 из моего проекта. (Нет неуважения, мне очень понравилось):)