Skip to content

Instantly share code, notes, and snippets.

@RitvikMandyam
Last active May 28, 2016 03:00
Show Gist options
  • Save RitvikMandyam/f0e2aec9b0c22651847e0d033d20f1d3 to your computer and use it in GitHub Desktop.
Save RitvikMandyam/f0e2aec9b0c22651847e0d033d20f1d3 to your computer and use it in GitHub Desktop.
public class BindingRecyclerAdapter extends RecyclerView.Adapter {
private List<Item> items;
public static class BindingHolder extends RecyclerView.ViewHolder {
private ViewDataBinding binding;
public BindingHolder(View rowView) {
super(rowView);
binding = DataBindingUtil.bind(rowView);
}
public ViewDataBinding getBinding() {
return binding;
}
}
public BindingRecyclerAdapter(List<Item> items) {
this.items = items;
}
@Override
public BindingHolder onCreateViewHolder(ViewGroup parent, int type) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_item, parent, false);
BindingHolder holder = new BindingHolder(v);
return holder;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder GenericHolder, int position) {
final Item item = items.get(position);
BindingHolder holder = (BindingHolder) GenericHolder;
holder.getBinding().setVariable(BR.item, book);
holder.getBinding().executePendingBindings();
}
@Override
public int getItemCount() {
return items.size();
}
}
public class CustomBindingAdapter {
@BindingAdapter("bind:imageUrl")
public static void loadImage(ImageView imageView, String url) {
Picasso.with(imageView.getContext()).load(url).into(imageView);
}
}
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="item"
type="com.example.databinding.objects.Item" />
</data>
<LinearLayout xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/item_thumbnail"
android:layout_width="80dp"
android:layout_height="100dp"
app:imageUrl="@{item.thumbnail}"
tools:src="@android:drawable/sym_def_app_icon" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:orientation="vertical">
<TextView
android:id="@+id/item_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@{item.title}"
tools:text="Title" />
<TextView
android:id="@+id/item_about"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@{item.about}"
tools:text="About" />
</LinearLayout>
</LinearLayout>
</layout>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment