我使用TokenAutoComplete创建收件人,所以我创建了带有图像的自定义视图,但是当我尝试将图像加载到其中时,它不起作用。我尝试使用 Picasso 和 Glide 以及远程和本地图像 url。它不会给出任何错误,只需加载占位符即可。
这是相关代码:
xml:
<data>
<import type="android.text.TextUtils" />
<import type="android.view.View" />
<import type="android.graphics.Typeface" />
<variable name="drawCircle" type="boolean" />
<variable name="imageUrl" type="String" />
</data>
<LinearLayout
android:id="@id/layout_content_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg_chip_token"
android:orientation="horizontal"
android:gravity="center_vertical"
android:paddingRight="@dimen/view_padding"
android:paddingEnd="@dimen/view_padding">
<ImageView
style="@style/AppStyle.Widget.ListItem.Image"
android:id="@+id/image_user" />
<TextView
android:id="@+id/text_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:layout_marginLeft="@dimen/view_margin"
android:layout_marginStart="@dimen/view_margin"
android:ellipsize="middle"
android:maxLines="1"
android:textColor="@android:color/white"
android:textAppearance="?android:attr/textAppearanceSmall" />
<ImageView
android:id="@+id/image_dismiss"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/view_margin"
android:layout_marginStart="@dimen/view_margin"
android:src="@drawable/ic_clear_white_24dp"
android:visibility="gone"/>
</LinearLayout>
风格:
<style name="AppStyle.Widget.ListItem.Image" parent="AppStyle.Widget.ImageView">
<item name="android:layout_width">@dimen/list_item_icon_size</item>
<item name="android:layout_height">@dimen/list_item_icon_size</item>
</style>
联系人完成视图:
公共类 ContactsCompletionView 扩展 TokenCompleteTextView {
private ChipTokenBinding mBinding;
public ContactsCompletionView(Context context) {
super(context);
}
public ContactsCompletionView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ContactsCompletionView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected View getViewForObject(final Recipient recipient) {
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
mBinding = DataBindingUtil.inflate(inflater, R.layout.chip_token, (ViewGroup) getParent(), false);
mBinding.textName.setText(recipient.getLabel());
ImageViewBindingAdapter.loadImage(mBinding.imageUser, recipient.getImageUrl(), true,
Drawables.getDrawable(getContext(), R.drawable.ic_account_circle_white_36dp));
return mBinding.getRoot();
}
@Override
protected Recipient defaultObject(String completionText) {
return null;
}
ImageViewBindingAdapter:
@BindingAdapter({"imageUrl", "circle", "error"})
public static void loadImage(ImageView view, String imageUrl, boolean circle, Drawable error) {
if (TextUtils.isEmpty(imageUrl) && error == null)
return;
Picasso.with(view.getContext())
.load(imageUrl)
.fit()
.transform(circle ? new CircleTransformation() : new NullTransformation())
.error(error)
.placeholder(error)
.into(view);
}