 |
|
|  |
[DataBinding] ImageView src¿¡ ¿¬µ¿ Çϱâ (ÇÔ¼ö ¿¬°á) |
|
|
 |
8³â Àü |
DataBindingÀÌ ¹«¾ùÀÌ°í ±âº»ÀûÀ¸·Î ¾î¶»°Ô ¾²ÀÌ´ÂÁö´Â ¾Æ·¡¿¡¼ È®ÀÎ ÇÏ¸é µÇ°Ú½À´Ï´Ù.
https://developer.android.com/topic/libraries/data-binding/index.html
À̹ø ³»¿ë¿¡¼´Â ImageView src¿¡ ¾î¶»°Ô À̹ÌÁö¸¦ ¿¬µ¿ ÇÒ °ÍÀΰ¡¿¡ ´ëÇØ °øÀ¯Çغ¸°íÀÚ ÇÕ´Ï´Ù.
¿ì¼± ImageView¿¡ À̹ÌÁö ¸®¼Ò½º id¸¦ ±×³É ¿¬°á ÇÏ¸é µÇÁö ¾Ê½À´Ï´Ù.
¾Æ·¡¿Í °°ÀÌ »ý°¢ÇϽô ºÐµéÀÌ ÀÖÀ» ²®´Ï´Ù.
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<data>
<variable
name="data"
type="com.example.Data"/>
</data>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@{data.resid}"/>
</layout>
¾ÈµË´Ï´Ù. ¾ÈµÇ¿ä~~
src¿¡ À̹ÌÁö¸¦ ³Ö±â À§Çؼ´Â BindAdapter annotationÀ» ÀÌ¿ëÇÏ¿© ÇÔ¼ö ¹ÙÀεùÀÌ ÇÊ¿ä ÇÕ´Ï´Ù.
Àü ¿©±â¼ °ÅÀÇ ¸î½Ã°£À» Å×½ºÆ® ÇÏ°í ½Ã°£À» º¸³Â´Âµ¥¿ä.
ÇÔ¼ö ¹ÙÀεùÀ» ÇÒ ¶§ °¡Àå ½±°Ô »ý°¢ ÇÒ °ÍÀÌ annotationÀ¸·Î ¿¬µ¿ÀÌ µÈ´Ù´Â °ÍÀÔ´Ï´Ù.
ÇÔ¼ö ¸¸µå´Â ¹ýÀ» °£´ÜÇÏ°Ô º¸¿©µå¸®°Ù½À´Ï´Ù.
package com.namuon.ringq.fragment.ring;
import android.databinding.BindingAdapter;
import android.widget.ImageView;
public class TestBindingAdapter {
@BindingAdapter({"bind:imgRes"})
public static void imgload(ImageView imageView, int resid) {
imageView.setImageResource(resid);
}
}
TestBindingAdapter¶ó´Â Ŭ·¡½º´Â ±×³É Àڹٴ Ŭ·¡½º ³»¿¡ Äڵ带 Àû¾î¾ß Çϱ⠶§¹®¿¡ ¸¸µé¾î ³õÀº °ÍÀ̶ó°í »ý°¢ÇÏ½Ã¸é µË´Ï´Ù.
±×³É ¸¶À½´ë·Î Ŭ·¡½º Çϳª »ý¼ºÇϽñ¸¿ä.
Áß¿äÇÑ ºÎºÐÀÌ imgload static ÇÔ¼ö ÀÔ´Ï´Ù.
public static void ·Î Á¤ÀÇ ÇϽŠ´ÙÀ½¿¡ Àû¿ë ÇÏ°í ½ÍÀ¸½Å View¸¦ ¸Å°³º¯¼ö ù¹ø°¿¡ ³Ö½À´Ï´Ù.
±×¸®°í µÎ¹ø° ¸Å°³º¯¼ö ºÎÅÍ´Â ¹Þ¾Æ¿À´Â °ªÀ» ³Ö½À´Ï´Ù.
±×¸®°í BindingAdapter annotationÀ¸·Î ÇÔ¼ö¸¦ Á¤ÀÇ ÇÕ´Ï´Ù.
"bind:imgRes" °¡ xml ¿¡¼ "app:imgRes"·Î »ç¿ëÇÒ ¿¹Á¤ ÀÔ´Ï´Ù.
¿©±â¼ appÀº ¾Æ·¡¿Í °°Àº xml ³×ÀÓ ½ºÆäÀ̽º ÁÒ.
xmlns:app="http://schemas.android.com/apk/res-auto"
imgRes¿Í imgload(ÇÔ¼ö¸í)Àº ²À °°Àº ÇÊ¿ä°¡ ¾ø½À´Ï´Ù. SpringÀ» ½áº¸½Å ºÐµéÀº ¾Æ½Ã°ÚÁö¸¸, ±×³É ÇÔ¼ö¸íÀÏ »ÓÀÌÁÒ ¤¾
·ÎÄÿ¡¼ ºÎ¸¦ ¶§´Â Àú ÇÔ¼ö¸¦ ºÎ¸£¸é µË´Ï´Ù.
ÀÌÁ¦ ÇÔ¼ö´Â Á¤ÀÇ µÇ¾úÀ¸´Ï XML¿¡ Àû¿ë ½ÃÄÑ º¸°Ú½À´Ï´Ù.
<?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"
android:layout_width="match_parent" android:layout_height="match_parent">
<data>
<variable
name="data"
type="com.example.Data"/>
</data>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:imgRes="@{data.resid}"/>
</layout>
ÀÚ À§¿Í °°ÀÌ ÇÏ¸é µË´Ï´Ù.
¾Æ·¡°¡ Áß½ÉÀÌ µÇ´Â ºÎºÐÀÌÁÒ.
app:imgRes="@{data.resid}"
annotationÀ¸·Î Á¤ÀÇµÈ À̸§À¸·Î Á¤ÀÇ ÇÑ ´ÙÀ½¿¡ ¹ÙÀεùÀ» ½ÃÅ°¸é µË´Ï´Ù.
ÀÌ·¸°Ô Ŭ·¡½º¿¡ static ÇÔ¼ö¸¦ BindingAdapter¶ó´Â annotation¸¸ Á¤ÀÇÇϸé
xml¿¡¼ ÆíÇÏ°Ô À̹ÌÁö¸¦ ºÎ¸¦ ¼öµµ ÀÖ½À´Ï´Ù.
¾Æ·¡´Â Âü°í·Î imageUrl ÇÔ¼ö·Î½á Glide¸¦ ÀÌ¿ëÇÏ´Â ¿¹Á¦ ÀÔ´Ï´Ù.
@BindingAdapter({"bind:imageUrl"})
public static void loadImage(ImageView imageView, String imageUrl) {
Glide.with(imageView.getContext())
.load(imageUrl)
.diskCacheStrategy(DiskCacheStrategy.SOURCE)
.placeholder(R.color.lighter_gray)
.into(imageView);
}
Now you can load the image from the string URL using the following method from XML:
<ImageView
android:layout_width="@dimen/place_item_image_size"
android:layout_height="@dimen/place_item_image_size"
app:imageUrl="@{viewModel.imageUrl}" />
http://mlsdev.com/en/blog/57-android-data-binding
±×·³ DataBindingÀ¸·Î Á»´õ ÆíÇÏ°Ô ÄÚµù Çغ¸¼¼¿ä. |
|
̵̧ : 887 |
̵̧
¸ñ·Ï
|
|
|  |
|