In my Activity
I have a GridView
inflated from this XML layout:
<GridView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="@dimen/thumbnail_size"
android:gravity="fill"
android:horizontalSpacing="16dp"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:verticalSpacing="16dp" />
The items inside it are RelativeLayout
s, each set to a fixed height. While profiling my Activity
, I noticed that when I enter an ActionMode
, it causes a measure pass, which causes the GridView
to be re-measured. The GridView
in turn calls getView()
on my adapter many times to measure all of the child views. This is unnecessary work, since (a) which children are visible hasn't changed, so it already has all the views it needs; and (b) none of the child views can change size.
I know that ListView
and GridView
have lots of tricks they can do to avoid work in easy cases, and this seems like the kind of special case they might have an optimization for. Is there something I can set on the GridView
to help it realise there's no need to call getView()
again here? Alternatively, can I go higher up the stack and avoid the measure pass completely when entering and leaving an ActionMode
? Just to be clear, the behaviour of my adapter is correct, and the getView()
calls aren't causing me a problem: I just want my app to burn fewer cycles.
N.B. Even if I set the items to have fixed width as well as height, and replace the GridView
with the following, it doesn't change the number of getView()
calls (but it does make my Activity
ugly).
<GridView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="beforeDescendants"
android:columnWidth="@dimen/thumbnail_size"
android:horizontalSpacing="16dp"
android:numColumns="3"
android:stretchMode="none"
android:verticalSpacing="16dp" />