Inconsistent Displaying of TextView in RecyclerView

Nathan Sokalski 4,111 Reputation points
2021-06-17T03:53:49.517+00:00

I have a RecyclerView that contains a list of integers for which I want to calculate and set the width. Here is the method I use to do this:

private void UpdateRoundNumbers()  
{  
	(this.rvRoundNumbers.GetAdapter() as RoundNumbersAdapter).RoundNumbers.Add(this.rvRoundNumbers.GetAdapter().ItemCount + 1);  
	this.rvRoundNumbers.GetAdapter().NotifyItemInserted(this.rvRoundNumbers.GetAdapter().ItemCount - 1);  
  
	TextView tv = new TextView(Application.Context, null, 0, Resource.Style.ItemTextView);  
	this.rvRoundNumbers.LayoutParameters.Width = (int)Enumerable.Range(1, ScorePadApplication.Players.Max((pd) => pd.Scores.Count) + 1).Max((roundnumber) => { return tv.Paint.MeasureText(roundnumber.ToString()); }) + tv.PaddingStart + tv.PaddingEnd;  
}  

However, the RecyclerView (the black column on the left in the screenshots) is not always displaying the added values, even though I call NotifyItemInserted. I have confirmed that the value calculated by the method is correct even when it does not display the new values, and when one value is not displayed, that continues for the rest of the debugging session. Here are screenshots of when it works & doesn't work:

106384-screenshot-1623899943.png

106385-screenshot-1623899982.png

I change no code between debug sessions, and yet it still happens inconsistently (the two screenshots were taken back to back by doing nothing other than starting a new debug session). What could be causing something like this? Thanks.

Developer technologies .NET Xamarin
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. JarvanZhang 23,971 Reputation points
    2021-06-18T02:40:54.533+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    It seems there is nothing wrong with the code that adding the new values for the recyclerView. The problem may be related to the rendering view. If you want to set adaptive width the recyclerView, you could just set the width of the recyclerView to wrap_content. Then it will adjust it's width automatically when adding new items..

       <androidx.recyclerview.widget.RecyclerView  
           android:id="@+id/test_recyclerView"  
           android:layout_width="wrap_content"  
           android:layout_height="wrap_content"/>  
    

    Best Regards,

    Jarvan Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.

    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.


Your answer

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