【问题标题】:How to create custom data binding in android? (android studio)如何在android中创建自定义数据绑定? (安卓工作室)
【发布时间】:2016-06-11 10:06:39
【问题描述】:

我想在下面的代码中实现自定义功能以从ImageView 下载图像,例如app:imageUrl="@{status.imageUrl}":

 <?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">

      <data>
        <variable
          name="status"
          type="com.databinding.data.Status" />

      </data>

      <RelativeLayout
        android:id="@+id/status_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ImageView
          android:id="@+id/status_avatar"
          android:layout_width="64dp"
          android:layout_height="64dp"
          android:layout_alignParentLeft="true"
          android:layout_alignParentStart="true"
          android:layout_alignParentTop="true"
          android:contentDescription="@null"
          app:imageUrl="@{status.imageUrl}"/>

      </RelativeLayout>
    </layout>

如何编写可以从@{status.imageUrl} 自动下载图像的函数? 使用这个库com.android.databinding。

【问题讨论】:

    标签: java android android-studio data-binding


    【解决方案1】:

    对于这项工作,您需要像 android databinding lib 这样的库。
    在这个库中,首先将以下脚本添加到项目的build.gradle:

    buildscript {
        repositories {
            jcenter()
        }
        dependencies {
            classpath 'com.android.tools.build:gradle:1.5.0'
            classpath 'com.android.databinding:dataBinder:1.0-rc4'
        }
    }
    

    并将此代码添加到模块文件的build.gradle 顶部:

    apply plugin: 'com.android.databinding'
    

    并创建您的类,例如:class BindingCustom 并编写以下代码:

    public class BindingCustom {
    
        @BindingAdapter({"imageUrl"})
        public static void loadImage(final ImageView view, String url) {
    
            Picasso.with(view.getContext()).load(url).into(view);
    
        }
    }
    

    在BindingCustom 类中,您有loadImage 方法可以通过您感兴趣的方式从URL 下载图像,但我使用Picasso 库,因为它是这项工作的常用库,您可以将其更改为您的代码。

    This is a helpful link for more information

    【讨论】:

      【解决方案2】:

      下面是我喜欢的:

      首先制作一个自定义类扩展表单图像视图

      import android.annotation.TargetApi;
      import android.content.Context;
      import android.os.Build;
      import android.util.AttributeSet;
      import android.widget.ImageView;
      
      public class MyImageView extends ImageView {
          public MyImageView(Context context) {
              super(context);
              downloader(null);
          }
      
          public MyImageView(Context context, AttributeSet attrs) {
              super(context, attrs);
              downloader(attrs);
          }
      
          public MyImageView(Context context, AttributeSet attrs, int defStyleAttr) {
              super(context, attrs, defStyleAttr);
              downloader(attrs);
          }
      
          @TargetApi(Build.VERSION_CODES.LOLLIPOP)
          public MyImageView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
              super(context, attrs, defStyleAttr, defStyleRes);
               downloader(attrs);
          }
      
          private void downloder(AttributeSet attr){
          // TAKE THE LINK AND DOWNLOAD IMAGE HERE
          }
      }
      

      第二个在你的 res 文件夹中声明一个 styleable

      <declare-styleable name="MyImageView">
          <attr name="imageUrl" format="string"/>
      </declare-styleable>
      

      终于让我们的下载器功能了

      private void downloader(AttributeSet attrs) {
          if (attrs!=null) {
              TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.MyImageView);
              String url = a.getString(R.styleable.MyImageView_imageUrl);
              // First check whether we have such a property then
              // DOWNLOAD IT WITH ANY LIBRARY YOU LIKE
              // in this case i used IMAGE LOADER
              if(url!=null)
                  ImageLoader.getInstance().displayImage(url,this);
          }
      }
      

      现在您可以轻松地在 xml 中添加链接

       <com.raianraika.example.MyImageView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          app:imageUrl="www.google.com"/>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-11-06
        • 2022-12-16
        • 2019-04-24
        • 1970-01-01
        • 1970-01-01
        • 2017-02-09
        • 2015-12-31
        • 1970-01-01
        相关资源
        最近更新 更多