Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 58056

Performance problem when changing Button text (Android)

$
0
0

I've built a simple CalendarView that uses Button instances to display the days of the month, as shown in the following screenshot:

ravib.com/download/calendar.png

When the navigation buttons at the top of the view are clicked, the calendar recomputes the dates of each day and updates the Text property of each Button. In Android, it takes a long time (~500 mSec) to change the text of the 42 buttons in the view. This occurs on the Genymotion emu as well as a physical device (Google Nexus 7).

The code to update the buttons is pretty straightforward:

this._dateLabels = ...;  // set date labels based on currently displayed month
Stopwatch sw = new Stopwatch;
foreach (int i=0; (i < this._dateButtons.Count); i++) {
    this._dateButtons.ElementAt(i).Text = this._dateLabels.ElementAt(i);  // slow!
}
sw.Stop();
TimeSpan elapsed = sw.Elapsed;    // elapsed is approx 500 mSec

The identical code is invoked when the calendar is shown for the first time (i.e. when each Button.Text value is initially null). However, in this case the measured elapsed time is between 0 and 2 mSec.

If I replace the slow line with:

    this._dateButtons.ElementAt(i).Text = "42";  // all buttons display "42"

the elapsed time for the loop is again between 0 and 2 mSec.

This leads me to believe there's a performance issue when Button.Text is changed from a non-null value to a different non-null value.

Is there a workaround to prevent each Button from recomputing any layout information it may be trying to recompute (a la Windows' SuspendLayout() and ResumeLayout()) when its Text is changed?

PS: I haven't been able to test this on iOS.


Viewing all articles
Browse latest Browse all 58056

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>