Skip to content

Instantly share code, notes, and snippets.

View ExampleFragment.java
public class ExampleFragment extends Fragment {
private FragmentExampleBinding binding;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, @Nullable Bundle savedInstanceState) {
return ViewBindingDelegate.inflate(
FragmentExampleBinding::inflate, b -> binding = b,
inflater, container, getViewLifecycleOwner());
@killvetrov
killvetrov / BaseViewBindingFragment.java
Last active May 3, 2021
Android View Binding: base class to reduce boilerplate in Java
View BaseViewBindingFragment.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;