Last active
July 7, 2022 07:15
-
-
Save elvismetaphor/52d70130c0f6f3c3dd6a3f57d6e5de01 to your computer and use it in GitHub Desktop.
Show how to implement MVVM with Data Binding while using Glide
This file contains 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" | |
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> |
This file contains 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
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); | |
} | |
} |
This file contains 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
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