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="viewModel" | |
| type="uk.ivanc.imagedatabinding.ProfileViewModel" /> | |
| </data> | |
| <RelativeLayout | |
| android:layout_width="match_parent" | |
| android:layout_height="match_parent"> |
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 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()) |
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
| protected void onCreate(Bundle savedInstanceState) { | |
| super.onCreate(savedInstanceState); | |
| ProfileActivityBinding binding = | |
| DataBindingUtil.setContentView(this, R.layout.profile_activity); | |
| binding.setViewModel(new ProfileViewModel(this)); | |
| } |
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"> | |
| <data> | |
| <variable | |
| name="viewModel" | |
| type="uk.ivanc.imagedatabinding.ProfileViewModel" /> | |
| </data> | |
| <RelativeLayout | |
| android:layout_width="match_parent" | |
| android:layout_height="match_parent"> | |
| <ImageView |
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 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()); |