Skip to content

Instantly share code, notes, and snippets.

@shakil807g
Created December 23, 2015 12:03
Show Gist options
  • Save shakil807g/2f365a0126250674d6f8 to your computer and use it in GitHub Desktop.
Save shakil807g/2f365a0126250674d6f8 to your computer and use it in GitHub Desktop.
DataBinding in Recycleview Android
public interface MovieClickHandler{
void onNewClick(View view);
void onWatchedClick(View view);
}
...
<data>
...
<variable name="click" type="com.example.databinding.MovieClickHandler" />
</data>
...
<ImageView
...
android:onClick="@{movie.isWatched ? click.onWatchedClick : click.onNewClick}"/>
...
public void onBindViewHolder(MovieItemViewHolder holder, int position) {
Movie movie = Movie.ITEMS[position];
holder.binding.setMovie(movie);
holder.binding.setClick(new MovieClickHandler() {
@Override
public void onWatchedClick(View view) {
}
@Override
public void onOldClick(View view) {
}
});
}
@BindingAdapter("bind:imageUrl")
public static void loadImage(ImageView imageView, String v) {
Picasso.with(imageView.getContext()).load(v).into(imageView);
}
app:imageUrl="@{movie.image}"
<ImageView
...
app:filter='@{movie.isWatched ? "grey" : null}'
.../>
@BindingAdapter("bind:filter")
public static void applyFilter(ImageView imageView, String v) {
imageView.setColorFilter(null);
if("grey".equals(v)){
ColorMatrix matrix = new ColorMatrix();
matrix.setSaturation(0);
ColorMatrixColorFilter cf = new ColorMatrixColorFilter(matrix);
imageView.setColorFilter(cf);
}
}
<TextView
...
android:text='@{movie.title ?? "unknown"}'
... />
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
</data>
<!-- Сюда добавляем свой layout -->
</layout>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="movie"
type="com.example.databinding.Movie" />
</data>
<!-- Сюда добавляем свой layout -->
</layout>
<?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="movie"
type="com.example.databinding.Movie" />
</data>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_margin="8dp">
<RelativeLayout
android:id="@+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/imageView"
...
app:imageUrl="@{movie.image}"/>
<TextView
android:id="@+id/textView"
...
android:text="@{movie.title}"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/textView2"
...
android:text="@{movie.description}"
android:textAppearance="?android:attr/textAppearanceSmall" />
</RelativeLayout>
</android.support.v7.widget.CardView>
</layout>
public class Movie {
public boolean isWatched;
public String image;
public String description;
public String title;
public Movie(boolean isWatched, String image, String description, String title) {
this.isWatched = isWatched;
this.image = image;
this.description = description;
this.title = title;
}
}
@Override
public void onBindViewHolder(MovieItemViewHolder holder, int position) {
Movie movie = Movie.ITEMS[position];
holder.title.setText(movie.title);
holder.description.setText(movie.description);
Picasso.with(holder.image.getContext()).load(movie.image).into(holder.image);
}
@Override
public void onBindViewHolder(MovieItemViewHolder holder, int position) {
Movie movie = Movie.ITEMS[position];
holder.binding.setMovie(movie);
}
@Override
public MovieItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
MovieItemBinding binding = MovieItemBinding.inflate(inflater, parent, false);
return new MovieItemViewHolder(binding.getRoot());
}
public class MovieItemViewHolder extends RecyclerView.ViewHolder {
MovieItemBinding binding;
public MovieItemViewHolder(View v) {
super(v);
binding = DataBindingUtil.bind(v);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment