2016-02-28 22 views
9

Tôi muốn thực hiện các chức năng tùy chỉnh để tải hình ảnh từ ImageView như app:imageUrl="@{status.imageUrl}" này trong mã dưới đây:Làm cách nào để tạo liên kết dữ liệu tùy chỉnh trong Android? (Android studio)

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

Làm thế nào để viết chức năng này có thể tải hình ảnh tự động từ một @{status.imageUrl}? Sử dụng thư viện này com.android.databinding.

Trả lời

4

Đối với tác phẩm này, bạn cần phải có một lib như android databinding lib.
Trong thư viện này, trước hết phải thêm bên dưới kịch bản để build.gradle của dự án:

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

Và thêm mã này để đứng đầu build.gradle của tập tin mô-đun:

apply plugin: 'com.android.databinding' 

Và tạo lớp học của bạn ví dụ: class BindingCustom và viết mã này:

public class BindingCustom { 

    @BindingAdapter({"imageUrl"}) 
    public static void loadImage(final ImageView view, String url) { 

     Picasso.with(view.getContext()).load(url).into(view); 

    } 
} 

Trong lớp BindingCustom bạn cóPhương phápđể tải xuống hình ảnh từ URL theo cách bạn quan tâm, nhưng tôi sử dụng thư viện Picasso vì đó là thư viện phổ biến cho công việc này và bạn có thể thay đổi nó thành mã của mình.

This is a helpful link for more information

2

dưới đây là những gì tôi thích:

trước tiên hãy nhìn hình ảnh dưới dạng lớp tùy chỉnh mở rộng

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 
    } 
} 

thứ hai khai báo một styleable trong thư mục res của bạn

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

cuối cùng cho phép làm cho chức năng downloader của chúng tôi

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); 
    } 
} 

bây giờ bạn có thể dễ dàng thêm các liên kết trong xml của bạn

<com.raianraika.example.MyImageView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    app:imageUrl="www.google.com"/> 
Các vấn đề liên quan