有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

我想用下面的代码实现从ImageView下载图像的自定义函数,如app:imageUrl="@{status.imageUrl}"

 <?xml version="1.0" encoding="utf-8"?>
    <layout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
      xmlns:app="http://schemas.安卓.com/apk/res-auto">

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

      </data>

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

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

      </RelativeLayout>
    </layout>

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


共 (2) 个答案

  1. # 1 楼答案

    以下是我喜欢的:

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

    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>
    

    最后让我们制作下载程序

    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"/>
    
  2. # 2 楼答案

    对于这项工作,您需要一个类似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