我在那里的图像是从网址下载并需要一些时间来加载图像的图片库。所以我想对进度。我尝试添加动画绘制并添加一个占位符,但在这种情况下,加载图像需要大空间的形象。我想占位符显示为一个正常的进度条。
码:
dot_selector
<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/selected_dot" android:state_selected="true"/>
<item android:drawable="@drawable/default_dot"/>
</selector>
progress_animation
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:gravity="center">
<animated-rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/loading"
android:padding="50dp"
android:pivotX="50%"
android:pivotY="50%" />
</item>
</layer-list>
我还通过添加监听下滑试过了,但在这里没有运气,因为我不能监听器类的内部访问viewholder元素。
using System;
using System.Collections.Generic;
using System.Linq;
using Android.App;
using Android.Support.V7.Widget;
using Android.Views;
using Android.Widget;
using Com.Bumptech.Glide;
using Com.Bumptech.Glide.Load.Resource.Drawable;
using Provenance.Models;
using Com.Bumptech.Glide.Request;
using Com.Bumptech.Glide.Request.Target;
using Com.Bumptech.Glide.Load;
using Com.Bumptech.Glide.Load.Engine;
using Java.Lang;
namespace Provenance.Android.Adapters
{
public class ViewDocumentAdapter : RecyclerView.Adapter, IRequestListener
{
#region Variable Declarations
public List<DocumentImageURL> _items;
private readonly Activity _context;
public event EventHandler<int> ItemClick;
#endregion
public ViewDocumentAdapter(Activity activity, IEnumerable<DocumentImageURL> images)
{
_items = images.ToList();
_context = activity;
}
void OnClick(int position)
{
if (ItemClick != null)
{
ItemClick(this, position);
}
}
public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
{
View itemView = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.imageListItem, parent, false);
DocumentViewHolder vh = new DocumentViewHolder(itemView, OnClick);
return vh;
}
public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
{
DocumentViewHolder vh = holder as DocumentViewHolder;
var chemical = _items[position];
// vh.imageView.SetImageResource(Resource.Drawable.logo);//_items[position]);
DrawableTransitionOptions options = new DrawableTransitionOptions();
options.CrossFade();
//.transition(DrawableTransitionOptions.withCrossFade())
Glide.With(_context)
.Load(_items[position].Thumbnail)
.Apply(new Com.Bumptech.Glide.Request.RequestOptions()
.Placeholder(Resource.Drawable.progress_animation)//Android.Resource.Drawable.ic_document)
.Error(Resource.Drawable.ic_exclaimation)
.FitCenter())
.Transition(options)
.Listener(this)
//.Apply(RequestOptions.CircleCropTransform())
.Into(vh.imageView);
}
public DocumentImageURL getSelectItem(int position)
{
return _items[position];
}
public bool OnLoadFailed(GlideException p0, Java.Lang.Object p1, ITarget p2, bool p3)
{
return false;
}
public bool OnResourceReady(Java.Lang.Object p0, Java.Lang.Object p1, ITarget p2, DataSource p3, bool p4)
{
///cant access progressbar<----
return false;
}
public override int ItemCount
{
get { return _items.Count; }
}
public class DocumentViewHolder : RecyclerView.ViewHolder
{
public ImageView imageView { get; set; }
public ProgressBar progress { get; set; }
// Get references to the views defined in the CardView layout.
public DocumentViewHolder(View itemView, Action<int> listener) : base(itemView)
{
imageView = itemView.FindViewById<ImageView>(Resource.Id.imageView);
progress = itemView.FindViewById<ProgressBar>(Resource.Id.progress);
itemView.Click += (sender, e) => listener(base.Position);
}
}
}
}
任何人都可以提出一个更好的选择。
我设法通过传递DocumentViewHolder
参数监听器类来解决这个问题:
修改后的代码:
public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
{
DocumentViewHolder vh = holder as DocumentViewHolder;
var chemical = _items[position];
DrawableTransitionOptions options = new DrawableTransitionOptions();
options.CrossFade();
Glide.With(_context)
.Load(_items[position].Thumbnail)
.Apply(new Com.Bumptech.Glide.Request.RequestOptions()
.Placeholder(Resource.Drawable.progress_animation)//Android.Resource.Drawable.ic_document)
.Error(Resource.Drawable.ic_exclaimation)
.FitCenter())
.Transition(options)
.Listener(new listenerImage(vh))
.Into(vh.imageView);
}
internal class listenerImage : Java.Lang.Object,IRequestListener
{
private DocumentViewHolder holder;
public listenerImage(DocumentViewHolder holder)
{
this.holder = holder;
}
public bool OnLoadFailed(GlideException p0, Java.Lang.Object p1, ITarget p2, bool p3)
{
return false;
}
public bool OnResourceReady(Java.Lang.Object p0, Java.Lang.Object p1, ITarget p2, DataSource p3, bool p4)
{
holder.progress.Visibility = ViewStates.Gone;
return false;
}
}