2016-07-25 99 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