【问题标题】:Databinding android update imageview数据绑定android更新imageview
【发布时间】:2016-07-25 08:44:17
【问题描述】:

您好,我正在尝试使用数据绑定更新 imageview,但我知道如何正确操作。

我的变量

@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}"/>

我有搜索但找不到解决方案.... 链接=link1Link2Link 3

【问题讨论】:

    标签: android data-binding android-databinding


    【解决方案1】:

    你使用了错误的属性来调用绑定方法,因为你已经定义了@BindingAdapter({"bind:someImage"}),你需要在从xml调用它时使用someImage

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

    【讨论】:

      【解决方案2】:

      您没有使用您在绑定适配器中设置的相同值,您设置了@BindingAdapter({"bind:someImage"}) 并使用了app:imageUrl

      改用这个:

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

      例子:

      @BindingAdapter("loadImageUrl")
      fun loadImageUrl(imageView: ImageView, url: String) {
          Glide.with(imgView).load(url)
              .placeholder(PLACE_HOLDER_IMAGE)
              .into(imageView)
      }
      
      
                      <ImageView
                          loadImageUrl="@{vm.url}"
                          android:layout_width="match_parent"
                          android:layout_height="match_parent" />
      

      【讨论】:

      • 虽然此代码可能会回答问题,但提供有关它如何和/或为什么解决问题的额外上下文将提高​​答案的长期价值。
      【解决方案3】:

      如果您的数据模型包含图像的资源 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 中(我在其中添加代码时无法格式化代码):

      android:src="@{ContextCompat.getDrawable(context,roomInfoItem.resId)}"

      其中 resId 包含 R.drawable.your_image_name

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-10-28
        • 1970-01-01
        • 2020-02-12
        • 1970-01-01
        • 2023-04-06
        • 2016-05-26
        • 1970-01-01
        相关资源
        最近更新 更多