0

我有一个自定义选择器,可以增加对话。

但是,当您从列表中选择一个项目,然后再次单击选择器时,列表将重新出现,对当前选择的项目没有任何指示。

iOS实际上是这样做的,但不是android。

这是我的 android 选择器对话框:

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

我该怎么做?

4

1 回答 1

1

您需要自定义AlertDialog,您可以在其中添加一个ListView

参考以下代码(样式需要自己定义):

AndroidPickerRenderer

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

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>

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>
于 2020-12-09T02:44:23.390 回答