Thomas
Thomas

Reputation: 2197

MvxSpinner loses focus after an item is selected if the spinner is inside a ListView

I have a ListView which generates its ListItems dynamically via a CustomAdapter in the view. The ListView holds different InputControls, say EditTexts, DatePickers, MvxSpinners and so on.

When a User selects an Item from an MvxSpinner the Focus is set to the first focusable InputControl beeing displayed on the screen.

How can I make sure that after the user selects an item from the Spinner the Spinner keeps or gets the focus?

This is the xml from the SpinnerListViewItem:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:local="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="left"
    >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Text"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:clickable="false"
        local:MvxBind="Text Beschriftung"
        style="@style/CardLabelMediumStyle" />
    <Mvx.MvxSpinner
        local:MvxBind="ItemsSource ItemsSource;SelectedItem SelectedItem"
        local:MvxItemTemplate="@layout/item_text_spinner"
        local:MvxDropDownItemTemplate="@layout/item_text_spinner_dropdown"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:clickable="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

code of the custom adapter:

    public class PruefpunkteAdapter : MvxAdapter
    {
        IDictionary<Type, TypeDescriptor> types = new Dictionary<Type, TypeDescriptor>();

        public PruefpunkteAdapter(Context context, IMvxAndroidBindingContext bindingContext)
            : base(context, bindingContext)
        {
            types.Add(typeof(FormularpunktTextBox), new TypeDescriptor() { TempldateId = Resource.Layout.ListItem_PruefberichtFormularpunktTextBox, ViewType = 5 });
            types.Add(typeof(FormularpunktSpinner), new TypeDescriptor() { TempldateId = Resource.Layout.ListItem_PruefberichtFormularpunktSpinner, ViewType = 15 });
            /* about 15 more types added */
        }

        public override int GetItemViewType(int position)
        {
            var item = GetRawItem(position);

            var descriptor = types[item.GetType()];
            return descriptor.ViewType;
        }

        public override int ViewTypeCount
        {
            get { return types.Count; }
        }

        protected override View GetBindableView(View convertView, object source, int templateId)
        {
            return base.GetBindableView(convertView, source, types[source.GetType()].TempldateId);
        }

        private class TypeDescriptor
        {
            public int ViewType;
            public int TempldateId;
        }
    }

Upvotes: 0

Views: 497

Answers (1)

PkL728
PkL728

Reputation: 955

In your custom adapter you could add code like this for the Spinner:

spinner.setFocusable(true);
spinner.setFocusableInTouchMode(true);
spinner.FocusChangeListener += (s, e) =>
{
    bool hasFocus = e.HasFocus;
    if (hasFocus) {
        YourActivity.this.spinner.performClick();
    }
}

Might be a little off on the "YourActivity.this.spinner" part of this without seeing how your code currently looks but basically just need to run performClick(); on your spinner.

Upvotes: 1

Related Questions