Created
December 23, 2015 12:03
-
-
Save shakil807g/2f365a0126250674d6f8 to your computer and use it in GitHub Desktop.
DataBinding in Recycleview Android
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | |
} | |
}); | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@BindingAdapter("bind:imageUrl") | |
public static void loadImage(ImageView imageView, String v) { | |
Picasso.with(imageView.getContext()).load(v).into(imageView); | |
} | |
app:imageUrl="@{movie.image}" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<TextView | |
... | |
android:text='@{movie.title ?? "unknown"}' | |
... /> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<layout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:app="http://schemas.android.com/apk/res-auto"> | |
<data> | |
</data> | |
<!-- Сюда добавляем свой layout --> | |
</layout> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Override | |
public void onBindViewHolder(MovieItemViewHolder holder, int position) { | |
Movie movie = Movie.ITEMS[position]; | |
holder.binding.setMovie(movie); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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()); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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