Simple data binding
So your FragmentBeatBoxBinding class has two references: getRoot(), which refers to the entire
layout, and recyclerView, which refers to just your RecyclerView (Figure 20.2).
Figure 20.2 Your binding class
Your layout only has one view, of course, so both references point at the same view: your
RecyclerView.
Now to use this binding class. Override onCreateView(...) in BeatBoxFragment and use
DataBindingUtil to inflate an instance of FragmentBeatBoxBinding (Listing 20.6). (You will
need to import FragmentBeatBoxBinding like any other class. If Android Studio cannot find
FragmentBeatBoxBinding, this means it was not autogenerated for some reason. Force Android Studio
to generate the class by selecting Build → Rebuild Project. If the class is not generated after forcing the
project to rebuild, restart Android Studio.)
Listing 20.6 Inflating a binding class (BeatBoxFragment.java)
public class BeatBoxFragment extends Fragment {
public static BeatBoxFragment newInstance() {
return new BeatBoxFragment();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
FragmentBeatBoxBinding binding = DataBindingUtil
.inflate(inflater, R.layout.fragment_beat_box, container, false);
return binding.getRoot();
}
}