Skip to content

Instantly share code, notes, and snippets.

@killvetrov
Last active November 15, 2022 12:58
Show Gist options
  • Save killvetrov/3998e60ddc2bf9d5955003688832eddb to your computer and use it in GitHub Desktop.
Save killvetrov/3998e60ddc2bf9d5955003688832eddb to your computer and use it in GitHub Desktop.
Android View Binding: base class to reduce boilerplate in Java
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("...");
}
}
@Jintin
Copy link

Jintin commented Dec 19, 2020

@killvetrov
Thanks for explain and sharing.

@rahmatsyam
Copy link

@killvetrov
Thanks for your sharing

@tonyradz
Copy link

tonyradz commented Apr 1, 2022

check my solution in Kotlin based on yours https://stackoverflow.com/a/71704997/5503940

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment