Skip to content

Instantly share code, notes, and snippets.

Iván Carballo ivacf

View GitHub Profile
@ivacf
ivacf / profile_activity.xml
Created Sep 25, 2015
Android layout file that uses data binding to load a image into an ImageView
View profile_activity.xml
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" >
<data>
<variable
name="viewModel"
type="uk.ivanc.imagedatabinding.ProfileViewModel" />
</data>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
@ivacf
ivacf / ProfileViewModel.java
Created Sep 25, 2015
View model that exposes a method using @BindingAdapter to load an image given a url
View ProfileViewModel.java
public class ProfileViewModel {
public String getImageUrl() {
// The URL will usually come from a model (i.e Profile)
return "http://cdn.meme.am/instances/60677654.jpg";
}
@BindingAdapter({"bind:imageUrl"})
public static void loadImage(ImageView view, String imageUrl) {
Picasso.with(view.getContext())
@ivacf
ivacf / ProfileActivity.java
Last active Sep 24, 2015
Activity that sets its content view using data binding and a view model
View ProfileActivity.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ProfileActivityBinding binding =
DataBindingUtil.setContentView(this, R.layout.profile_activity);
binding.setViewModel(new ProfileViewModel(this));
}
@ivacf
ivacf / profile_activity.xml
Created Sep 24, 2015
Android layout that uses data binding to load a drawable into an ImageView
View profile_activity.xml
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable
name="viewModel"
type="uk.ivanc.imagedatabinding.ProfileViewModel" />
</data>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
@ivacf
ivacf / ProfileViewModel.java
Last active Apr 22, 2018
View model that downloads an image using Picasso and data binding
View ProfileViewModel.java
public class ProfileViewModel {
// The URL will usually come from a model (i.e Profile)
static final String IMAGE_URL = "http://cdn.meme.am/instances/60677654.jpg";
public ObservableField<Drawable> profileImage;
private BindableFieldTarget bindableFieldTarget;
public ProfileViewModel(Context context) {
profileImage = new ObservableField<>();
// Picasso keeps a weak reference to the target so it needs to be stored in a field
bindableFieldTarget = new BindableFieldTarget(profileImage, context.getResources());
You can’t perform that action at this time.