0
votes

I have a custom picker that inflates a dialoge.

But when you pick an item from the list and then click on the picker again, the list will reappear, giving NO indication over currently picked item.

iOS does this actually, but not android.

This is my android picker dialog:

 private void Control_Click(object sender, EventArgs e)
        {
            //throw new NotImplementedException();
            Picker model = Element;
            // Element


            string[] items = model.Items.ToArray();
            AlertDialog.Builder listDialog =
                new AlertDialog.Builder(context);
            listDialog.SetTitle(model.Title ?? "");

            listDialog.SetItems(items, (sender2, args) =>
            {
           
                ElementController.SetValueFromRenderer(Picker.SelectedIndexProperty, args.Which);
                if (Element != null)
                {
                    if (model.Items.Count > 0 && Element.SelectedIndex >= 0)
                        Control.Text = model.Items[Element.SelectedIndex];
                    ElementController.SetValueFromRenderer(VisualElement.IsFocusedProperty, false);
                    Control?.ClearFocus();
                }
                listDialog = null;
            });

            listDialog.SetNegativeButton("Zurück", (s, a) =>
            {
                ElementController.SetValueFromRenderer(VisualElement.IsFocusedProperty, false);
                Control?.ClearFocus();
                listDialog = null;
            });


            listDialog.Show();
        }

How would I do this?

1

1 Answers

1
votes

You need to custom AlertDialog,you could add a ListView into it.

refer to the below codes (styles need to be defined by you):

the AndroidPickerRenderer class:

public  class AndroidPickerRenderer : PickerRenderer
{
    private Context context;
    AlertDialog listDialog;
    string[] items;
    public AndroidPickerRenderer(Context context) : base(context)
    {
        this.context = context;
    }
    protected override void OnElementChanged(ElementChangedEventArgs<Picker> e)
    {
        base.OnElementChanged(e);
        if (Control !=null)
        {
            Control.Click += Control_Click;
        }
    }
   
    private void Control_Click(object sender, EventArgs e)
    {
            Picker model = Element;
            items = model.Items.ToArray();    
            AlertDialog.Builder builder =
           new AlertDialog.Builder(context);
            builder.SetTitle(model.Title ?? "");
            builder.SetNegativeButton("Zurück", (s, a) =>
            {
                Control?.ClearFocus();
                builder = null;
            });
            Android.Views.View view = LayoutInflater.From(context).Inflate(Resource.Layout.listview, null);
            ListView listView = view.FindViewById<ListView>(Resource.Id.listView1);

            MyAdapter myAdapter = new MyAdapter(items, Element.SelectedIndex);
            listView.Adapter = myAdapter;
            listView.ItemClick += ListView_ItemClick;
            builder.SetView(view);
            listDialog = builder.Create();        
            listDialog.Show();
    }

    private void ListView_ItemClick(object sender, AdapterView.ItemClickEventArgs e)
    {
        Control.Text = items[e.Position];
        Element.SelectedIndex = e.Position;
        listDialog.Dismiss();
        listDialog = null;
    }

    class MyAdapter : BaseAdapter
    {
        private string[] items;
        private int selectedIndex;

        public MyAdapter(string[] items)
        {
            this.items = items;
        }

        public MyAdapter(string[] items, int selectedIndex) : this(items)
        {
            this.selectedIndex = selectedIndex;
        }

        public override int Count => items.Length;

        public override Java.Lang.Object GetItem(int position)
        {
            return items[position];
        }

        public override long GetItemId(int position)
        {
            return position;
        }

        public override Android.Views.View GetView(int position, Android.Views.View convertView, ViewGroup parent)
        {
            if (convertView == null)
            {
                convertView = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.listview_item, null);
            }
            TextView textView = convertView.FindViewById<TextView>(Resource.Id.textView1);
            textView.Text = items[position];
            if (position == selectedIndex)
            {
                textView.SetBackgroundColor(Color.Red.ToAndroid()); //highlight color
            }
            else
            {
                textView.SetBackgroundColor(Color.White.ToAndroid());//default color
            }
            return convertView;
        }
    }
}

the listview.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="match_parent"
   android:layout_height="match_parent">
    <ListView
       android:id="@+id/listView1"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"/>

 
</LinearLayout>

the listview_item.xml :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="match_parent"
   android:layout_height="match_parent">
    <TextView
       android:id="@+id/textView1"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"/>
</LinearLayout>