Data Binding Support Library
Provides useful subclasses from Support Library that work with Data Binding.
Download
repositories {
jcenter()
maven {
url "https://jitpack.io"
}
}
dependencies {
compile 'ooo.oxo:data-binding-support:1.1.1'
compile 'com.android.support:appcompat-v7:23.2.1'
compile 'com.android.support:recyclerview-v7:23.2.1'
}
Usage
BindingAppCompatActivity
and BindingFragment
public class MainActivity extends BindingAppCompatActivity<MainActivityBinding> {
@Override
protected int provideContentView(@Nullable Bundle savedInstanceState) {
return R.layout.main_activity;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// do anything you want, for example:
binding.setUser(user);
}
}
public class MainFragment extends BindingFragment<ProfileFragmentBinding> {
@Override
public int provideContentView(@Nullable Bundle savedInstanceState) {
return R.layout.profile_fragment;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
// do anything you want, for example:
binding.setUser(user);
}
}
BindingRecyclerView
public class PhotoAdapter extends BindingRecyclerView.ListAdapter<Photo, PhotoAdapter.ViewHolder> {
public PhotoAdapter(Context context, ObservableList<Photo> data) {
super(context, data);
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new ViewHolder(inflater, parent);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.binding.setItem(data.get(position));
}
public class ViewHolder extends BindingRecyclerView.ViewHolder<PhotoItemBinding> {
public ViewHolder(LayoutInflater inflater, ViewGroup parent) {
super(inflater, R.layout.photo_item, parent);
}
}
}
private final ObservableList photos = new ObservableArrayList();
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/* ... */
recyclerView.setAdapter(new PhotoAdapter(this, photos));
// Just add your items in the ObservableList, BindingRecyclerView.ListAdapter will take care of the
// binding for you.
photos.addAll(/* ... */);
}
@Override protected void onDestroy() {
super.onDestroy();
/* ... */
// Remember to release the adapter, because it has a reference to the `photos` ObservableList.
recyclerView.setAdapter(null);
}