-
-
Save killvetrov/3998e60ddc2bf9d5955003688832eddb to your computer and use it in GitHub Desktop.
public abstract class BaseViewBindingFragment extends Fragment { | |
private Field bindingField; | |
private Method inflate; | |
{ | |
try { | |
for (Field declaredField : this.getClass().getDeclaredFields()) { | |
if (ViewBinding.class.isAssignableFrom(declaredField.getType())) { | |
bindingField = declaredField; | |
bindingField.setAccessible(true); | |
for (Method method : bindingField.getType().getMethods()) { | |
if (method.getParameterTypes().length == 3) { | |
inflate = method; | |
break; | |
} | |
} | |
break; | |
} | |
} | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
@Nullable | |
@Override | |
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, | |
@Nullable Bundle savedInstanceState) { | |
try { | |
ViewBinding binding = (ViewBinding) inflate.invoke(null, inflater, container, false); | |
bindingField.set(this, binding); | |
return binding.getRoot(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
return super.onCreateView(inflater, container, savedInstanceState); | |
} | |
@Override | |
public void onDestroyView() { | |
try { | |
bindingField.set(this, null); | |
} catch (IllegalAccessException e) { | |
e.printStackTrace(); | |
} | |
super.onDestroyView(); | |
} | |
} |
public class ExampleFragment extends BaseViewBindingFragment { | |
// Just one declaration and it's done | |
private FragmentExampleBinding binding; | |
@Override | |
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { | |
binding.textView.setText("..."); | |
} | |
} |
@ricardopereira I've updated the code to make it ProGuard-friendly. inflate
method as well as ViewBinding field are looked up by signature rather then by name.
Have doubt on this a bit, if ViewBinding provide new method have the same number of parameter. Isn't it much easier to break?
And given inflate
is method out of the library itself, will proguard affect it?
btw, I also have few library using the same way to doing things like this.
https://github.com/Jintin/BindingExtension/ - Provide Activity/Fragment with ViewBinding using generic easily.
https://github.com/carousell/MonoAdapter/ - General adapter for singleViewType.
Hope you like it as well, haha.
@Jintin inflate
is not a library method, it is generated code. It's the same as if you add a class with inflate
method in it, so ProGuard will minify it by default. And yes, I agree that passing inflate method as reference has its benefits. As for solutions in Kotlin, I think the most optimal I saw is by @Zhuinden https://github.com/Zhuinden/ViewBindingExample/tree/master/app/src/main/java/com/zhuinden/viewbindingexample
@killvetrov
Thanks for explain and sharing.
@killvetrov
Thanks for your sharing
check my solution in Kotlin based on yours https://stackoverflow.com/a/71704997/5503940
Reading
inflate
property using reflection won't work if you have Proguard activated.