Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Fragment class for using data binding to replace view inflation.
package foo.bar;
import android.databinding.ViewDataBinding;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* Fragment class for using data binding to replace view inflation.
*/
public abstract class DataBindingFragment<B extends ViewDataBinding> extends Fragment {
private B binding;
@NonNull
public abstract B onCreateBinding(LayoutInflater inflater, ViewGroup container, Bundle
savedInstanceState);
@Nullable
@Override
public final View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
savedInstanceState) {
binding = onCreateBinding(inflater, container, savedInstanceState);
return binding.getRoot();
}
public void onBindingCreated(@NonNull B binding, @Nullable Bundle savedInstanceState) {
}
@Override
public final void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
onBindingCreated(binding, savedInstanceState);
}
@NonNull
public B getBinding() {
return binding;
}
public void onDestroyBinding() {
}
@Override
public final void onDestroyView() {
onDestroyBinding();
super.onDestroyView();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment