Android Programming The Big Nerd Ranch Guide by Bill Phillips, Chris Stewart, Kristin Marsicano (z-lib.org)

(gtxtreme123) #1
Simple data binding

Next, create a SoundHolder wired up to list_item_sound.xml.


Listing 20.9  Creating SoundHolder (BeatBoxFragment.java)


public class BeatBoxFragment extends Fragment {
public static BeatBoxFragment newInstance() {
return new BeatBoxFragment();
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
...
}


private class SoundHolder extends RecyclerView.ViewHolder {
private ListItemSoundBinding mBinding;


private SoundHolder(ListItemSoundBinding binding) {
super(binding.getRoot());
mBinding = binding;
}
}
}


Your SoundHolder expects the binding class you just implicitly created: ListItemSoundBinding.


Next, create an Adapter hooked up to SoundHolder. (If you put your cursor on RecyclerView.Adapter
before typing in any of the methods below and hit Option+Return [Alt+Enter], Android Studio will
enter most of this code for you.)


Listing 20.10  Creating SoundAdapter (BeatBoxFragment.java)


public class BeatBoxFragment extends Fragment {
...
private class SoundHolder extends RecyclerView.ViewHolder {
...
}


private class SoundAdapter extends RecyclerView.Adapter {
@Override
public SoundHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(getActivity());
ListItemSoundBinding binding = DataBindingUtil
.inflate(inflater, R.layout.list_item_sound, parent, false);
return new SoundHolder(binding);
}


@Override
public void onBindViewHolder(SoundHolder holder, int position) {


}


@Override
public int getItemCount() {
return 0;
}
}
}

Free download pdf