Lazily inflate entries in ExpandingEntryCardView

When first initializing ExpandingEntryCardView, don't inflate all
its subviews.

Change-Id: I7696ed4c327a6dbc6dc5e7e7fb81e8910234284c
diff --git a/res/layout/expanding_entry_card_item.xml b/res/layout/expanding_entry_card_item.xml
index 5f2deeb..888d457 100644
--- a/res/layout/expanding_entry_card_item.xml
+++ b/res/layout/expanding_entry_card_item.xml
@@ -14,74 +14,66 @@
      limitations under the License.
 -->
 
-<LinearLayout
+<RelativeLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
+    style="@style/SelectableItem"
+    android:id="@+id/entry_layout"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
-    android:orientation="horizontal">
+    android:paddingLeft="@dimen/expanding_entry_card_item_padding_with_image_start"
+    android:paddingRight="@dimen/expanding_entry_card_item_padding_end"
+    android:paddingTop="@dimen/expanding_entry_card_item_padding_top"
+    android:paddingBottom="@dimen/expanding_entry_card_item_padding_bottom">
 
-    <RelativeLayout
-        style="@style/SelectableItem"
-        android:id="@+id/entry_layout"
+    <ImageView
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
-        android:layout_weight="1"
-        android:paddingLeft="@dimen/expanding_entry_card_item_padding_with_image_start"
-        android:paddingRight="@dimen/expanding_entry_card_item_padding_end"
-        android:paddingTop="@dimen/expanding_entry_card_item_padding_top"
-        android:paddingBottom="@dimen/expanding_entry_card_item_padding_bottom">
+        android:id="@+id/icon"
+        android:layout_alignParentLeft="true"
+        android:layout_alignParentTop="true"
+        android:layout_marginRight="@dimen/expanding_entry_card_item_image_spacing"/>
 
-        <ImageView
-            android:layout_width="wrap_content"
-            android:layout_height="wrap_content"
-            android:id="@+id/icon"
-            android:layout_alignParentLeft="true"
-            android:layout_alignParentTop="true"
-            android:layout_marginRight="@dimen/expanding_entry_card_item_image_spacing"/>
+    <TextView
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:id="@+id/header"
+        android:layout_alignParentTop="true"
+        android:layout_toRightOf="@+id/icon"
+        android:textStyle="bold"
+        android:textColor="@android:color/black"
+        android:singleLine="true"/>
 
-        <TextView
-            android:layout_width="wrap_content"
-            android:layout_height="wrap_content"
-            android:id="@+id/header"
-            android:layout_alignParentTop="true"
-            android:layout_toRightOf="@+id/icon"
-            android:textStyle="bold"
-            android:textColor="@android:color/black"
-            android:singleLine="true"/>
+    <TextView
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:id="@+id/sub_header"
+        android:layout_below="@+id/header"
+        android:layout_toRightOf="@+id/icon_sub_header"
+        android:textColor="@android:color/black"/>
 
-        <TextView
-            android:layout_width="wrap_content"
-            android:layout_height="wrap_content"
-            android:id="@+id/sub_header"
-            android:layout_below="@+id/header"
-            android:layout_toRightOf="@+id/icon_sub_header"
-            android:textColor="@android:color/black"/>
+    <ImageView
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:id="@+id/icon_sub_header"
+        android:layout_below="@+id/header"
+        android:layout_toRightOf="@+id/icon"
+        android:layout_marginRight="@dimen/expanding_entry_card_item_sub_header_icon_margin_right"
+        android:layout_marginBottom="@dimen/expanding_entry_card_item_sub_header_icon_margin_bottom" />
 
-        <ImageView
-            android:layout_width="wrap_content"
-            android:layout_height="wrap_content"
-            android:id="@+id/icon_sub_header"
-            android:layout_below="@+id/header"
-            android:layout_toRightOf="@+id/icon"
-            android:layout_marginRight="@dimen/expanding_entry_card_item_sub_header_icon_margin_right"
-            android:layout_marginBottom="@dimen/expanding_entry_card_item_sub_header_icon_margin_bottom" />
+    <TextView
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:id="@+id/text"
+        android:layout_below="@+id/sub_header"
+        android:layout_toRightOf="@+id/icon_text"
+        android:textColor="@android:color/darker_gray"/>
 
-        <TextView
-            android:layout_width="wrap_content"
-            android:layout_height="wrap_content"
-            android:id="@+id/text"
-            android:layout_below="@+id/sub_header"
-            android:layout_toRightOf="@+id/icon_text"
-            android:textColor="@android:color/darker_gray"/>
-
-        <ImageView
-            android:layout_width="wrap_content"
-            android:layout_height="wrap_content"
-            android:id="@+id/icon_text"
-            android:layout_toRightOf="@+id/icon"
-            android:layout_below="@+id/sub_header"
-            android:layout_marginTop="@dimen/expanding_entry_card_item_text_icon_margin_top"
-            android:layout_marginRight="@dimen/expanding_entry_card_item_text_icon_margin_right" />
-    </RelativeLayout>
-
-</LinearLayout>
+    <ImageView
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:id="@+id/icon_text"
+        android:layout_toRightOf="@+id/icon"
+        android:layout_below="@+id/sub_header"
+        android:layout_marginTop="@dimen/expanding_entry_card_item_text_icon_margin_top"
+        android:layout_marginRight="@dimen/expanding_entry_card_item_text_icon_margin_right" />
+</RelativeLayout>
diff --git a/src/com/android/contacts/quickcontact/ExpandingEntryCardView.java b/src/com/android/contacts/quickcontact/ExpandingEntryCardView.java
index 0309030..18703d1 100644
--- a/src/com/android/contacts/quickcontact/ExpandingEntryCardView.java
+++ b/src/com/android/contacts/quickcontact/ExpandingEntryCardView.java
@@ -24,29 +24,20 @@
 import android.content.Intent;
 import android.content.res.Resources;
 import android.graphics.drawable.Drawable;
-import android.net.Uri;
-import android.provider.ContactsContract;
-import android.provider.ContactsContract.QuickContact;
-import android.support.v4.text.TextUtilsCompat;
-import android.support.v4.view.ViewCompat;
 import android.text.TextUtils;
 import android.util.AttributeSet;
 import android.util.Log;
 import android.view.LayoutInflater;
 import android.view.View;
-import android.view.View.OnClickListener;
 import android.view.ViewGroup;
 import android.view.ViewTreeObserver;
 import android.view.ViewTreeObserver.OnPreDrawListener;
-import android.view.animation.AccelerateDecelerateInterpolator;
-import android.widget.FrameLayout;
 import android.widget.ImageView;
 import android.widget.LinearLayout;
 import android.widget.TextView;
 
 import java.util.ArrayList;
 import java.util.List;
-import java.util.Locale;
 
 /**
  * Display entries in a LinearLayout that can be expanded to show all entries.
@@ -128,6 +119,7 @@
     private OnClickListener mOnClickListener;
     private boolean mIsExpanded = false;
     private int mCollapsedEntriesCount;
+    private List<Entry> mEntries;
     private List<View> mEntryViews;
     private LinearLayout mEntriesViewGroup;
     private int mThemeColor;
@@ -144,12 +136,7 @@
     };
 
     public ExpandingEntryCardView(Context context) {
-        super(context);
-        LayoutInflater inflater = LayoutInflater.from(context);
-        View expandingEntryCardView = inflater.inflate(R.layout.expanding_entry_card_view, this);
-        mEntriesViewGroup = (LinearLayout)
-                expandingEntryCardView.findViewById(R.id.content_area_linear_layout);
-        mTitleTextView = (TextView) expandingEntryCardView.findViewById(R.id.title);
+        this(context, null);
     }
 
     public ExpandingEntryCardView(Context context, AttributeSet attrs) {
@@ -170,12 +157,15 @@
             boolean isExpanded, int themeColor) {
         LayoutInflater layoutInflater = LayoutInflater.from(getContext());
         mIsExpanded = isExpanded;
-        mEntryViews = createEntryViews(layoutInflater, entries);
-        mThemeColor = themeColor;
+        mEntries = entries;
+        mEntryViews = new ArrayList<View>(entries.size());
         mCollapsedEntriesCount = Math.min(numInitialVisibleEntries, entries.size());
+        mThemeColor = themeColor;
+
         if (mExpandCollapseButton == null) {
             createExpandButton(layoutInflater);
         }
+        inflateViewsIfNeeded(layoutInflater);
         insertEntriesIntoViewGroup();
     }
 
@@ -220,7 +210,7 @@
         }
 
         removeView(mExpandCollapseButton);
-        if (mCollapsedEntriesCount < mEntryViews.size()
+        if (mCollapsedEntriesCount < mEntries.size()
                 && mExpandCollapseButton.getParent() == null) {
             addView(mExpandCollapseButton, -1);
         }
@@ -282,12 +272,15 @@
         mExpandCollapseButton.setOnClickListener(mExpandCollapseButtonListener);
     }
 
-    private List<View> createEntryViews(LayoutInflater layoutInflater, List<Entry> entries) {
-        ArrayList<View> views = new ArrayList<View>(entries.size());
-        for (Entry entry : entries) {
-            views.add(createEntryView(layoutInflater, entry));
+    /**
+     * Lazily inflate the number of views currently needed, and bind data from
+     * mEntries into these views.
+     */
+    private void inflateViewsIfNeeded(LayoutInflater layoutInflater) {
+        final int viewsToInflate = mIsExpanded ?  mEntries.size() : mCollapsedEntriesCount;
+        for (int i = mEntryViews.size(); i < viewsToInflate; i++) {
+            mEntryViews.add(createEntryView(layoutInflater, mEntries.get(i)));
         }
-        return views;
     }
 
     private View createEntryView(LayoutInflater layoutInflater, Entry entry) {
@@ -360,6 +353,8 @@
         final int startingHeight = mEntriesViewGroup.getHeight();
 
         mIsExpanded = true;
+        // In order to insert new entries, we may need to inflate them for the first time
+        inflateViewsIfNeeded(LayoutInflater.from(getContext()));
         insertEntriesIntoViewGroup();
         updateExpandCollapseButton(getCollapseButtonText());