Working with AlertDialog in Xamarin Android is implemented as well as natively +/- nuances. Below is one of the options how to do it. The code has not been tested !. Sorry for the design
- We go to NuGet and swing Square.Picasso.
- Immediately update Square.OkHttp. So that with the latest versions of Xamarin was not a problem.
Next, Content View (custom_dialog_with_img.xml).
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ffffff">
<android.support.v7.widget.RecyclerView android:id="@+id/my_items" android:layout_width="match_parent" android:layout_height="match_parent" android:clickable="true" android:fillViewport="true" android:scrollbars="vertical" />
</RelativeLayout>
Next, the Item View (item_with_img.xml).
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/layout" android:layout_width="match_parent" android:layout_height="40dp" android:orientation="horizontal" android:weightSum="2"> <RelativeLayout android:id="@+id/img_block" android:layout_width="30dp" android:layout_height="35dp" android:layout_gravity="end|center_vertical" android:layout_weight="0.02" android:orientation="vertical"> <android.support.v7.widget.AppCompatImageView android:id="@+id/img" android:layout_width="20dp" android:layout_height="20dp" android:layout_centerInParent="true"/> </RelativeLayout> <android.support.v7.widget.AppCompatTextView android:id="@+id/name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="left|center_vertical" android:layout_marginLeft="6dp" android:layout_marginRight="6dp" android:layout_weight="1.98" android:textColor="@color/primary_text_color" android:textSize="16sp" android:textStyle="bold" /> </LinearLayout>
- Create an Adapter
public class CustomAdapterForDialogWithList : BaseAdapter<Model> { private readonly Context _context; private readonly IList<Model> _items; private readonly int _layoutId;
public CustomAdapterForDialogWithList(Context context, IList<Model> items, int layoutId) { _context = context; _items = items; _layoutId = layoutId; } /// <summary> /// Получить ид из списка /// </summary> /// <param name="position"></param> /// <returns></returns> public override long GetItemId(int position) { return _items[position].Id; } /// <summary> /// /// </summary> /// <param name="position"></param> /// <param name="convertView"></param> /// <param name="parent"></param> /// <returns></returns> public override View GetView(int position, View convertView, ViewGroup parent) { ViewHolder holder; if (convertView == null) { var inflater = (LayoutInflater)_context.GetSystemService(Context.LayoutInflaterService); convertView = inflater.Inflate(_layoutId, null, true); holder = new ViewHolder { TitleView = (AppCompatTextView)convertView.FindViewById(Resource.Layout.name), ImageView = (AppCompatImageView)convertView.FindViewById(Resource.Layout.img) }; convertView.SetTag(_layoutId, holder); } else { holder = (ViewHolder)convertView.GetTag(_layoutId); holder.TitleView.Text = _items[position].Title; Picasso.With(_context).Load(_items[position].Url).Placeholder(Resource.Drawable.error).Error(Resource.Drawable.error).Into(holder.ImageView); } return convertView; } /// <summary> // /// Количество элементов в списке /// </summary> public override int Count => _items?.Count ?? 0; /// <summary> /// Получить элемент из списка зная его индекс /// </summary> /// <param name="position"></param> /// <returns></returns> public override Model this[int position] => _items[position]; } public class ViewHolder : Java.Lang.Object { /// <summary> /// Текстовое поле /// </summary> public AppCompatTextView TitleView { get; set; } /// <summary> /// Изображение /// </summary> public AppCompatImageView ImageView { get; set; } } }
public class Model { public int Id { get; set; } public string Title { get; set; } public string Url { get; set; } }
And last
var dialog = new AlertDialog.Builder(_context); var inflater = (LayoutInflater)_context.GetSystemService(Context.LayoutInflaterService); var view = inflater.Inflate(Resource.Layout.custom_dialog_list_mobile, null); var recycleView = view.FindViewById<RecyclerView>(Resource.Id.my_items); recycleView.HasFixedSize = true; var adapter = new CustomAdapterForDialogWithNumberList(_context, new Model() {...}, Resource.Layout.custom_dialog_with_img); recycleView.SetAdapter(adapter); dialog.SetView(view); dialog.Show();
Ps. BaseAdapter remake on RecyclerView.Adapter
getViewmethod of the adapter. It also displays the picture in its container - YuriySPb ♦