2016-07-25 102 views
0

嗨我試圖用數據綁定更新imageview,但我分得到如何做到這一點。數據綁定android更新imageview

我VAR

@BindingAdapter({"bind:someImage"}) 
     public void loadIt(ImageView view, String imageUrl) { 
      Picasso.with(view.getContext()) 
        .load(getSomeImage()) 
        .placeholder(android.R.drawable.alert_dark_frame) 
        .into(view); 
} 

public void setSomeImage(){ 
     notifyPropertyChanged(BR.someImage); 
    } 

我的佈局

<ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/imagePath" 
     app:imageUrl="@{var.someImage}"/> 

我有搜索,但無法找到解決辦法.... 鏈接= link1Link2,您使用的Link 3

回答

3

錯誤的屬性來調用綁定方法,因爲你已經定義了@BindingAdapter({"bind:someImage"})你需要使用someImage,而從XML

<ImageView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/imagePath" 
    app:someImage="@{var.someImage}"/> 
0

調用它,如果你的數據模型包含圖像的資源ID可以做到這一點沒有編程做任何事情。

例子:

<layout> 

<data> 
<import type="android.support.v4.content.ContextCompat" />    
<variable 
name="roomInfoItem" 
type="com.example......model.RoomInfoModel" /> </data> 

<RelativeLayout> 

<ImageView 
    android:id="@+id/iv_room_info_item" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
/> 

</RelativeLayout> 

</layout> 

只是將下面的行添加到您的ImageView(我無法格式化,當我在那裏將它的代碼):

機器人:SRC =「@ {ContextCompat.getDrawable(上下文,roomInfoItem.resId)}」

其中渣油包含R.drawable.your_image_name