I started using ViewBinding. After searching for some example or advice how to use ViewBinding with an abstract base class that should handle same logic on views expected to be present in every child's layout, I endet up posting this question here.
Scenario:
I have a base class public abstract class BaseFragment
. There are multiple Fragments that extend this base class. These Fragments have common views that are handled from the base class implementation (with the "old" findViewById()
). For example every fragment's layout is expected to contain a TextView with ID text_title. Here's how it's handled from the BaseFragment
's onViewCreated()
:
TextView title = view.findViewById(R.id.text_title);
// Do something with the view from the base class
Now the ViewBinding-API generates binding-classes for each child-Fragment. I can reference the views using the binding. But I can't using the concrete Bindings from the base class. Even with introducing generics to the base class there are too many types of fragment-bindings that I discarded this solution for now.
What's the recommended way of handling the binding's views from the abstract base class? Are there any best-practices? Didn't found a built-in mechanism in the API to handle this scenario in an elegant way.
When the child-fragments are expected to contain common views, I could provide abstract methods that return the views from the concrete bindings of the Fragments and make them accessible from the base class. (For example protected abstract TextView getTitleView();
). But is this an advantage rather than using findViewById()
? What do you think? Any other (better) solutions? Please let's start some discuss.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…