Xamarin.Android 设置BaseAdapter 的基础公共类

https://blog.csdn.net/sinat_26562875/article/details/51447785

 

 

  /// <summary>
    ///     通用适配器
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class CommonAdapter<T> : BaseAdapter<T>
    {
        private readonly List<T> items;
        private readonly Activity context;
        private readonly int layoutId;
 
        public delegate View GetViewEvent(
            int position, View convertView, ViewGroup parent, T item, ViewHolder viewHolder);
 
        /// <summary>
        ///     加载item的View事件
        /// </summary>
        public event GetViewEvent OnGetView;
 
 
        public CommonAdapter(Activity context, int layoutId, List<T> items)
        {
            this.context = context;
            this.layoutId = layoutId;
            this.items = items;
        }
 
 
        public override T this[int position]
        {
            get { return items[position]; }
        }
 
        public override int Count
        {
            get { return items.Count; }
        }
 
        public override long GetItemId(int position)
        {
            return position;
        }
 
        public override View GetView(int position, View convertView, ViewGroup parent)
        {
            var item = items[position];
            var view = convertView;
            var viewHolder = ViewHolder.Get(context, convertView, parent, layoutId, position);
 
            if (OnGetView != null)
                return OnGetView(position, convertView, parent, item, viewHolder);
            return view;
        }
    }

 

/// <summary>
    ///     通用ViewHolder
    /// </summary>
    public class ViewHolder : Object
    {
        private readonly SparseArray<View> views;
        private int position;
        private readonly View ConvertView;
 
        public ViewHolder(Activity context, ViewGroup parent, int layoutID, int position)
        {
            this.position = position;
            views = new SparseArray<View>();
            ConvertView = context.LayoutInflater.Inflate(layoutID, null);
            ConvertView.Tag = this;
        }
 
        //单例模式获取对象实例
        public static ViewHolder Get(Activity context, View convertView,
            ViewGroup parent, int layoutId, int position)
        {
            if (convertView == null)
                return new ViewHolder(context, parent, layoutId, position);
            var holder = (ViewHolder) convertView.Tag;
            holder.position = position;
            return holder;
        }
 
        public View GetConvertView()
        {
            return ConvertView;
        }
 
 
        /// <summary>
        ///     通过ViewID获取控件
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="viewId"></param>
        /// <returns></returns>
        public T GetView<T>(int viewId) where T : View
        {
            var view = views.Get(viewId);
            if (view == null)
            {
                view = ConvertView.FindViewById<T>(viewId);
                views.Put(viewId, view);
            }
 
            return (T) view;
        }
 
 
 
        /// <summary>
        ///     普通ImageView设置图片
        /// </summary>
        /// <param name="viewId"></param>
        /// <param name="bm"></param>
        /// <returns></returns>
        public ViewHolder SetImageBitmap(int viewId, Bitmap bm)
        {
            var view = GetView<ImageView>(viewId);
            if (view != null)
                view.SetImageBitmap(bm);
            return this;
        }
     
 
        ///     为任意View设置监听
        /// </summary>
        /// <typeparam name="T">View下所有的控件类型</typeparam>
        /// <param name="viewId">控件ID</param>
        /// <param name="widget">控件 View</param>
        /// <param name="listener"></param>
        /// <returns></returns>
        public ViewHolder SetOnClickListener<T>(int viewId, T widget,
            View.IOnClickListener listener) where T : View
        {
            View view = GetView<T>(viewId);
            view.SetOnClickListener(listener);
            return this;
        }
    }

 

 

使用方法

 private ControlExtend.CommonAdapter<Response_GetCheckOrderBillcode> _adapter

 

 _adapter = new ControlExtend.CommonAdapter<Response_GetCheckOrderBillcode>(当前Context对象, Resource.Layout.布局名称, List集合);

 

posted @ 2022-04-06 14:59  LuoCore  阅读(41)  评论(0编辑  收藏  举报