使用滑行添加进度条

时间:2019-02-06 09:38:43

标签: android xamarin.android

我有一个图片库,该图片库是从url加载的,需要一些时间来加载图像。所以我想进度条。我尝试添加一个可绘制的动画并添加一个占位符,但是在这种情况下,加载程序图像占用的空间与图像的空间相同。我希望将占位符显示为正常进度条。

代码:

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

谁能建议一个更好的选择。

1 个答案:

答案 0 :(得分:0)

我设法通过将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;
    }
}