如何通过滑行(或其他方式)将图像加载到listView项的小部件中

时间:2019-05-06 11:39:36

标签: android image android-widget android-glide remoteview

我有一个带有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);
    });

它可以工作,但是有些奇怪。它从屏幕上删除了其他元素,并使背景透明。

enter image description here

但必须像这样工作(仅左侧带有图标):

enter image description here

我该怎么办?谢谢

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

2 个答案:

答案 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();
    }