I found the code for Android studio, and I can’t figure out how to rewrite it under Visual studio Xamarin Android. I tried to rewrite this code here: http://dev.androidteam.ru/snippets/dialog/items-with-images-in-dialog For a whole day I have been trying to figure out how to implement this, but it doesn’t work out. Under Xamarin, there seems to be little in Russian. AlertDialog with a list is easy to create, but how to add pictures to the list? I hope to explain how to implement this - I really want to understand. Thank you in advance.

Список элементов с картинками

  • Each list uses an adapter. Each adapter uses markup for a list item. You need to use your adapter + your markup in the getView method of the adapter. It also displays the picture in its container - YuriySPb
  • Is there an example of such a list in Xamarin? I tried to figure out how to create an adapter, but it didn't work out for me. - user246016
  • I do not know how to Xamarin. Only Java, only hardcore) - Yuriy SPb

1 answer 1

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

  1. We go to NuGet and swing Square.Picasso.
  2. Immediately update Square.OkHttp. So that with the latest versions of Xamarin was not a problem.
  3. 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>

  4. 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> 
    1. 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; } }

  5. 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

  • Thanks for the example. I have already found similar code only for ListView. I just don’t understand why using Square.Picasso is probably some kind of additional component. I will rewrite this example for myself. - user246016
  • ListView is considered obsolete. Square.Picasso is a third-party image download component. More convenient than. What is available out of the box. - Alexey
  • I did not find the code where Square.Picasso is used in this code, so I did not use it. The adapter must be created RecyclerView.Adapter, but it has a completely different code for the adapter. The demo code helped a lot here: developer.xamarin.com/guides/android/user_interface/… - user246016
  • The call for dialogue is somewhat different for me, because You want to create mLayoutManager = new LinearLayoutManager (this); recycleView.SetLayoutManager (mLayoutManager); Here is my fully working example of such an alertDialog: yadi.sk/d/8KPhXd1k3L6ShU - can anyone come in handy or will there be comments on the code. - user246016