我正在尝试从drawable文件夹加载图像,而我正在使用数据绑定。图片未加载。
此文件的XML文件是
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable
name="viewModel"
type="com.example.user.finaldatabinder.ProfileViewModel"/>
<variable
name="user"
type="com.example.user.finaldatabinder.User"/>
</data>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#E0E0E0">
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="150dp"
card_view:cardCornerRadius="6dp"
android:layout_margin="4dp"
android:elevation="4dp"
android:background="#ffffff">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<View
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_alignParentRight="true"
android:id="@+id/dot"
android:background="@{user.isOnline ? @drawable/is_online : @drawable/is_offline }"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:id="@+id/image"
android:src="@{viewModel.imageUrl}"/>
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{user.name+' '+user.surname}"
android:id="@+id/username"
android:layout_toRightOf="@+id/image"
android:textSize="30dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{user.status}"
android:layout_toRightOf="@+id/image"
android:layout_below="@+id/username"
android:id="@+id/status"
android:textSize="20dp"
android:layout_marginBottom="10dp"/>
<Button
android:layout_width="150dp"
android:layout_height="wrap_content"
android:id="@+id/button1"
android:layout_below="@+id/status"
android:layout_marginLeft="30dp"
android:layout_marginRight="10dp"
android:background="#F57C00"
android:textColor="#ffffff"
android:gravity="center_horizontal"/>
<Button
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_below="@+id/status"
android:textColor="#ffffff"
android:background="#F57C00"
android:layout_toRightOf="@+id/button1"/>
<Button
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_below="@+id/status"
android:textColor="#ffffff"
android:background="#F57C00"
android:layout_toRightOf="@+id/button1"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
</RelativeLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
</layout>
我正在使用BindingAdapter的ProfileViewModel类是
package com.example.user.finaldatabinder;
import android.databinding.BindingAdapter;
import android.widget.ImageView;
import com.squareup.picasso.Picasso;
public class ProfileViewModel {
public String getImageUrl() {
// The URL will usually come from a model (i.e Profile)
return "drawable//mark";
}
@BindingAdapter({"bind:imageUrl"})
public static void loadImage(ImageView view, String imageUrl) {
Picasso.with(view.getContext())
.load(imageUrl)
.into(view);
}
}
和我正在执行DataBinding的MainActivity类是
package com.example.user.finaldatabinder;
import android.databinding.DataBindingUtil;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.example.user.finaldatabinder.databinding.ActivityMainBinding;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActivityMainBinding binding= DataBindingUtil.setContentView(this, R.layout.activity_main);
User user=new User("Mark","Zuckerberg","CEO of Facebook", false, false);
binding.setUser(user);
binding.setViewModel(new ProfileViewModel());
}
}
但是当我写作时
android: src="@drawable/mark"
图像加载完美。