我有一个带有ListView
的小部件。我尝试将列表项中的图片加载到ImageView
var uri = Uri.Parse(item.UrlPreviewImage);
var widgetTarget = new AppWidgetTarget(_context, Resource.Id.imageView_widgetListItemRss_rssIcon, itemView, _widgetId);
Xamarin.Essentials.MainThread.BeginInvokeOnMainThread(() =>
{
Glide.With(_context.ApplicationContext)
.AsBitmap()
.Load(uri)
.Into(widgetTarget);
});
它可以工作,但是有些奇怪。它从屏幕上删除了其他元素,并使背景透明。
但必须像这样工作(仅左侧带有图标):
我该怎么办?谢谢
P.S。 C#中的代码,但Kotlin或Java中的代码也将有用
更多代码:
public RemoteViews GetViewAt(int position)
{
var itemView = new RemoteViews(_context.PackageName, Resource.Layout.widget_list_item_rss);
var item = _list[position];
var subTitle = item.UpdateTime == null
? Strings.RssFeedItemNotUpdated
: $"{Strings.RssFeedItemUpdated} {item.UpdateTime.Value.ToShortGeneralLocaleString()}";
var countMessages = item.CountNewMessages.ToString();
itemView.SetTextViewText(Resource.Id.textView_widgetListItemRss_title, item.Name);
itemView.SetTextViewText(Resource.Id.textView_widgetListItemRss_subtitle, subTitle);
itemView.SetTextViewText(Resource.Id.textView_widgetListItemRss_rssCount, countMessages);
if (!string.IsNullOrEmpty(item.UrlPreviewImage))
{
var uri = Uri.Parse(item.UrlPreviewImage);
var widgetTarget = new AppWidgetTarget(_context, Resource.Id.imageView_widgetListItemRss_rssIcon, itemView, _widgetId);
Xamarin.Essentials.MainThread.BeginInvokeOnMainThread(() =>
{
Glide.With(_context.ApplicationContext)
.AsBitmap()
.Load(uri)
.Into(widgetTarget);
});
}
return itemView;
}
答案 0 :(得分:0)
我用毕加索解决它
if (!string.IsNullOrEmpty(item.UrlPreviewImage))
{
try
{
var picture = Picasso.With(_context).Load(item.UrlPreviewImage).Get();
itemView.SetImageViewBitmap(Resource.Id.imageView_widgetListItemRss_rssIcon, picture);
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
答案 1 :(得分:0)
对于Glide ver.4,我使用:
FutureTarget<Bitmap> futureTarget = Glide.with(context)
.asBitmap()
.load("picture URL")
.submit(250, 250);
try {
row.setImageViewBitmap(R.id.yourImageViewInItemView, futureTarget.get());
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}