Skip to content

Instantly share code, notes, and snippets.

@elvismetaphor
Last active July 7, 2022 07:15
Show Gist options
  • Save elvismetaphor/52d70130c0f6f3c3dd6a3f57d6e5de01 to your computer and use it in GitHub Desktop.
Save elvismetaphor/52d70130c0f6f3c3dd6a3f57d6e5de01 to your computer and use it in GitHub Desktop.
Show how to implement MVVM with Data Binding while using Glide
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable
name="viewModel"
type="edu.self.viewmodel.MainViewModel"/>
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="edu.self.view.MainActivity">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:imageUrl="@{viewModel.resultImageUrl}"/>
</LinearLayout>
</layout>
package edu.self.binding;
import android.databinding.BindingAdapter;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
public class CustomSetter {
@BindingAdapter("bind:imageUrl")
public static void loadImage(ImageView view, String url) {
Glide.with(view.getContext()).load(url).into(view);
}
}
package edu.self.viewmodel;
import android.databinding.ObservableField;
public class MainViewModel {
public ObservableField<String> resultImageUrl = new ObservableField<>();
public MainViewModel() {}
public void imageUrlUpdated(String url) {
resultImageUrl.set(url);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment