如何在安卓(Android)中创建自定义数据绑定?(使用安卓工作室(Android Studio))

9
我希望能够实现自定义函数来从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

2个回答

4

对于这项工作,您需要使用类似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库,因为它是一种常见的库,用于这项工作并且您可以将其更改为您自己的代码。 这是一个有用的链接,提供更多信息。

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接