Is Spinner PerformClick broken on exception throw?

Pedro_MonizPedro_Moniz Member ✭✭
edited January 15 in Xamarin.Android

Hello,

I was implementing a pager which would focus a spinner on swipe. These spinners are on viewholders inside a recycler view, nothing special.

    public void OnFocusChange(View v, bool hasFocus)
    {
        try
        {
            if (hasFocus)
            {
                _spinner.PerformClick();
            }
        }catch(Exception e)
        {
            //We add a listener to perform a click when added
            _spinner.AddOnAttachStateChangeListener(this);
        }
    }

On focus it would automatically be clicked. If the Spinner is not on screen yet (which happens with swipe) an exception is thrown and I will wait until it is attached to perform the click.

However, the spinner is now broken. The options will appear but when I choose one, OnItemSelected is not called and the spinner does not show the chosen option.
Also when I NotifySet this recicler view the layout gives an error stating that Android was still doing an operation.

If I comment the performclick and stop the mechanism for automatic spinner click, the spinner will work properly.

Any suggestion on how to handle this situation?

For the time being I have a special case to not automatically focus in case of a spinner but this is obviously not optimal.

Answers

  • DimChrisDimChris USMember ✭✭✭

    Your question is not very clear, please show us some images about what you are trying to do. Your code doesn't seem right as you handle it. But i need to understand more things about what you are trying to do.

  • Pedro_MonizPedro_Moniz Member ✭✭
    edited January 16

    I will attach some images for context.

    I have a pager and on page swipe the first element of the recicler view on the new page will be requested a focus.

    OnFocusChange will be called which will in turn call the onAttach and the app performs the automatic spinner click

    As demonstrated in the pictures the spinner is broken as I select an item but no onitemselected is called and no item appears written.

    On another swipe I will get exceptions depending on what kind of process I do.
    On a notifyDataSetChanged I get this:
    Unhandled Exception:
    Java.Lang.IllegalStateException: Cannot call this method while RecyclerView is computing a layout or scrolling

    Also take notice that the system works for edit texts and other UI elements well.

    Code snippets:

    On swipe we notify the page fragment to focus the correct element:

        private RecyclerView _pageContent;
    
        internal void GiveFocusToFieldIndex(int position)
        {
            _pageContent.ScrollToPosition(position);
            View row er_pageContent.GetChildAt(position);
    
        //If I add an ifCase for spinner, everything works fine.
    
                row.RequestFocus();
        }
    

    Code on viewholder to handle focus

        public void OnFocusChange(View v, bool hasFocus)
        {
            try
            {
                if (hasFocus)
                {
                    _spinner.PerformClick();
                }
            }catch(Exception)
            {
                //We add a listener to perform a click when added
                _spinner.AddOnAttachStateChangeListener(this);
            }
        }
    
    
        public void OnViewAttachedToWindow(View attachedView)
        {
            if (attachedView.Id == _spinner.Id)
            {
                _spinner.PerformClick();
                _spinner.RemoveOnAttachStateChangeListener(this);
            }
        }
    
  • YorkGoYorkGo CNMember, Xamarin Team Xamurai
    edited January 17

    On a notifyDataSetChanged I get this: Unhandled Exception...

    Try use Handler to call the notifyDataSetChanged:

    Handler handler = new Handler(Looper.MainLooper);
    Action action = () =>
    {
        notifyDataSetChanged();
    };
    handler.Post(action);
    
  • Pedro_MonizPedro_Moniz Member ✭✭
    edited January 17

    Please focus on the question.

    The spinner is already broken before the notify does the computing exception.

    The notify was just a simple example of a follow up exception from the spinner being broken.

  • Pedro_MonizPedro_Moniz Member ✭✭
    edited January 17

    I have solved my problem although the problem with the spinner still persists.

    Instead of triggering the perform click and handle the exception, I change a boolean in my view holder.

    On attach, if the boolean is true, I do the automatic click.

Sign In or Register to comment.