Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/195.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在android中创建自定义数据绑定?(安卓工作室)_Java_Android_Android Studio_Data Binding - Fatal编程技术网

Java 如何在android中创建自定义数据绑定?(安卓工作室)

Java 如何在android中创建自定义数据绑定?(安卓工作室),java,android,android-studio,data-binding,Java,Android,Android Studio,Data Binding,我想实现自定义函数,从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">

我想实现自定义函数,从
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

对于这项工作,您需要一个类似的库。
在此库中,首先将以下脚本添加到项目的
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'
并创建您的类,例如:
classbindingscustom
并编写以下代码:

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下载图像,但我使用该库,因为它是此作业的常用库,您可以将其更改为代码


以下是我喜欢的:

首先创建一个自定义类扩展表单图像视图

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文件夹中声明一个可设置样式的文件

<declare-styleable name="MyImageView">
    <attr name="imageUrl" format="string"/>
</declare-styleable>
现在,您可以轻松地在xml中添加链接

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

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