gsala
04/04/2019, 11:08 AMval button : Button by lazy {
findViewById(R.id.button)
}
ribesg
04/04/2019, 11:13 AMsindrenm
04/04/2019, 11:14 AMgsala
04/04/2019, 11:20 AMribesg
04/04/2019, 11:22 AMFragment
it’s wrong, but inside an Activity
it may be okgsala
04/04/2019, 11:23 AMribesg
04/04/2019, 11:23 AMhttps://developer.android.com/images/fragment_lifecycle.png▾
ghosalmartin
04/04/2019, 11:26 AMsindrenm
04/04/2019, 11:28 AMprivate val button: Button
get() = the_button_in_the_layout // synthetically imported
gsala
04/04/2019, 11:29 AMprivate val rootView : View by lazy { inflateView() }
override fun onCreateView(): View{
return rootView
}
Would this be bad practice ?ribesg
04/04/2019, 11:34 AMgsala
04/04/2019, 11:36 AMribesg
04/04/2019, 11:37 AMval button : Button
get() = findViewById(R.id.button)
But in this case you may end up calling button
multiple times in the same function like it’s nothing. Well, it’s not huge, but it’s better to have a local var and call findViewById
only once if you cangsala
04/04/2019, 11:39 AMribesg
04/04/2019, 11:41 AMgsala
04/04/2019, 11:44 AMsindrenm
04/04/2019, 11:47 AMribesg
04/04/2019, 11:48 AMsindrenm
04/04/2019, 11:49 AMsignInEmailAddressEditText
), so I prefer to keep track of them in property getters. 😒imple_smile:ribesg
04/04/2019, 11:52 AMgsala
04/04/2019, 11:54 AMribesg
04/04/2019, 11:54 AMButterKnife.bind(this);
after the view has been created to replace the instances by the ones for the newly created view, which dereferences old views in the processsindrenm
04/04/2019, 11:58 AMribesg
04/04/2019, 11:58 AMsindrenm
04/04/2019, 12:02 PMribesg
04/04/2019, 12:14 PMsindrenm
04/04/2019, 12:24 PMribesg
04/04/2019, 12:37 PMclass MainView(override val ctx: Context) : Ui {
override val root = constraintLayout ()
}
sindrenm
04/04/2019, 12:40 PMribesg
04/04/2019, 12:42 PMclass MainView(override val ctx: Context) : Ui {
val myTextView =
textView {
textResource = R.string.my_string
}
override val root = constraintLayout {
add(myTextView, lParams {
centerInParent()
})
}
}
Splitties is better than Anko, it works differently and fixes some general issues.
And for complex views, you can very easily split them in multiple files.
It’s also a lot better to just have your views there, like myTextView
in my example. It’s just there. In my Activity/Fragment I just have view.myTextView
.
Also parsing XML is something not-trivial in terms of resources 😛sindrenm
04/04/2019, 12:52 PMcenterInParent()
, for instance. But the preview is not as good as it is with XML layouts.
Also, the XML files aren't shipped as XML, it's binary. 😒imple_smile:ribesg
04/04/2019, 1:07 PMsindrenm
04/04/2019, 1:08 PMzhuinden
04/04/2019, 3:58 PMLou Morda
04/04/2019, 10:22 PMrkeazor
04/05/2019, 3:11 AMLou Morda
06/27/2019, 11:59 PMrkeazor
06/28/2019, 2:52 AM