Skip to content

Instantly share code, notes, and snippets.

@heinrichreimer
Created March 21, 2017 15:42
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save heinrichreimer/bc0de87a98693fef0b047f4d9351e69e to your computer and use it in GitHub Desktop.
Save heinrichreimer/bc0de87a98693fef0b047f4d9351e69e to your computer and use it in GitHub Desktop.
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