Share via

RequestLayout() Causing Infinite Loop For LayoutChange Event

Nathan Sokalski 4,111 Reputation points
2021-11-28T22:56:30.03+00:00

I have the following EventHandler for the LayoutChange event of a GridLayout:

private void incEditPlayerNames_LayoutChange(object sender, LayoutChangeEventArgs e)
{
    this.incEditPlayerNames.LayoutChange -= this.incEditPlayerNames_LayoutChange;
    if (this.IsKeyboardOpen() && Resources.Configuration.Orientation == Android.Content.Res.Orientation.Landscape)
    {
        this.incEditPlayerNames.GetChildAt(0).Visibility = ViewStates.Gone;
        this.btnSavePlayerNames.Visibility = ViewStates.Gone;
        this.btnCancelChangesPlayerNames.Visibility = ViewStates.Gone;
    }
    else
    {
        this.incEditPlayerNames.GetChildAt(0).Visibility = ViewStates.Visible;
        this.btnSavePlayerNames.Visibility = ViewStates.Visible;
        this.btnCancelChangesPlayerNames.Visibility = ViewStates.Visible;
    }
    this.rvPlayerNamesInput.RequestLayout();
    this.incEditPlayerNames.LayoutChange += this.incEditPlayerNames_LayoutChange;
}

Notice that at the beginning & end of the handler I detach & reattach the handler, and that I call RequestLayout() on a RecyclerView inside the GridLayout (incEditPlayerNames is a GridLayout & rvPlayerNamesInput is a RecyclerView). I need to call RequestLayout() in order to rerender the GridLayout. If I do not call it, the RecyclerView does not get resized for the new layout (the layout changes are the Visibility properties set in the code above, as well as whether or not the keyboard is open). However, if I do call it, the app seems to appear & function correctly, but it ends up in an infinite loop of calling the LayoutChange event. I'm not sure how to make everything get rerendered correctly without calling RequestLayout(), but as I said, it causes an infinite loop (which would probably cause some kind of problem if continued to let it run). What should I do?

Developer technologies | .NET | Xamarin
0 comments No comments

Answer accepted by question author

JarvanZhang 23,971 Reputation points
2021-11-29T07:08:59.687+00:00

Hello,​

Welcome to our Microsoft Q&A platform!

if I do call it, the app seems to appear & function correctly, but it ends up in an infinite loop of calling the LayoutChange event.

This is because calling the recyclerView.RequestLayout would trigger the parent layout's LayoutChanged event. To avoid this, try adding a bool parameter to prevent calling the code multi times.

Here is the sample code, you could refer to it.

   bool statusValue = true;  
   private void incEditPlayerNames_LayoutChange(object sender, LayoutChangeEventArgs e)  
   {  
       if (statusValue)  
       {  
           if (this.IsKeyboardOpen() && Resources.Configuration.Orientation == Android.Content.Res.Orientation.Landscape)  
           {  
               this.incEditPlayerNames.GetChildAt(0).Visibility = ViewStates.Gone;  
               this.btnSavePlayerNames.Visibility = ViewStates.Gone;  
               this.btnCancelChangesPlayerNames.Visibility = ViewStates.Gone;  
           }  
           else  
           {  
               this.incEditPlayerNames.GetChildAt(0).Visibility = ViewStates.Visible;  
               this.btnSavePlayerNames.Visibility = ViewStates.Visible;  
               this.btnCancelChangesPlayerNames.Visibility = ViewStates.Visible;  
           }  
     
           this.rvPlayerNamesInput.RequestLayout();  
           statusValue = false;  
       }  
       else  
       {  
           statusValue = true;  
       }  
   }  

Best Regards,

Jarvan Zhang


If the response is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

Was this answer helpful?

0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.