I have a view with 4 containers (LinearLayouts). Each container will be filled with content depending on data. (Each content view is inflated with a layout and added to the container)
Previously I called ButterKnife.bind(this, view)
after I inflated each of the layouts, when the data was loaded. Now I want to get rid of ButterKnife in favor of the kotlinx synthetic view binding.
My problem is, the first time the view is created and setup everything works fine. When I refresh the view, most of the views are null (which weren't null before and shouldn't be).
Some code:
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)
}
Layout:
<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>
Is there anything I can do about that view mess? Maybe 'recall' the view binding?