From 3857d7aeb320476419d81ecfa1895bbe769a0bc6 Mon Sep 17 00:00:00 2001 From: Winson Chung Date: Mon, 4 May 2015 15:28:34 -0700 Subject: [PATCH] Fixing crash in All Apps. Bug 20431579 Change-Id: Iba6ce88a931cb56f111f5b2ea44f81c5059a934f --- .../android/launcher3/AppsGridAdapter.java | 75 +++++++++++-------- 1 file changed, 45 insertions(+), 30 deletions(-) diff --git a/src/com/android/launcher3/AppsGridAdapter.java b/src/com/android/launcher3/AppsGridAdapter.java index 954c59ffce..5bc3981dfc 100644 --- a/src/com/android/launcher3/AppsGridAdapter.java +++ b/src/com/android/launcher3/AppsGridAdapter.java @@ -33,13 +33,13 @@ class AppsGridAdapter extends RecyclerView.Adapter { */ public static class ViewHolder extends RecyclerView.ViewHolder { public View mContent; - public boolean mIsSectionRow; + public boolean mIsSectionHeader; public boolean mIsEmptyRow; - public ViewHolder(View v, boolean isSectionRow, boolean isEmptyRow) { + public ViewHolder(View v, boolean isSectionHeader, boolean isEmptyRow) { super(v); mContent = v; - mIsSectionRow = isSectionRow; + mIsSectionHeader = isSectionHeader; mIsEmptyRow = isEmptyRow; } } @@ -72,36 +72,26 @@ class AppsGridAdapter extends RecyclerView.Adapter { @Override public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) { List items = mApps.getAdapterItems(); - if (items.isEmpty()) { - return; - } - for (int i = 0; i < parent.getChildCount(); i++) { View child = parent.getChildAt(i); ViewHolder holder = (ViewHolder) parent.getChildViewHolder(child); - if (holder != null) { - GridLayoutManager.LayoutParams lp = (GridLayoutManager.LayoutParams) - child.getLayoutParams(); - if (!holder.mIsSectionRow && !holder.mIsEmptyRow && !lp.isItemRemoved()) { - if (items.get(holder.getPosition() - 1).isSectionHeader) { - // Draw at the parent - AlphabeticalAppsList.AdapterItem item = - items.get(holder.getPosition()); - String section = item.sectionName; - mSectionTextPaint.getTextBounds(section, 0, section.length(), - mTmpBounds); - if (mIsRtl) { - int left = parent.getWidth() - mPaddingStart - mStartMargin; - c.drawText(section, left + (mStartMargin - mTmpBounds.width()) / 2, - child.getTop() + (2 * child.getPaddingTop()) + - mTmpBounds.height(), mSectionTextPaint); - } else { - int left = mPaddingStart; - c.drawText(section, left + (mStartMargin - mTmpBounds.width()) / 2, - child.getTop() + (2 * child.getPaddingTop()) + - mTmpBounds.height(), mSectionTextPaint); - } - } + if (shouldDrawItemSection(holder, child, items)) { + // Draw at the parent + AlphabeticalAppsList.AdapterItem item = + items.get(holder.getPosition()); + String section = item.sectionName; + mSectionTextPaint.getTextBounds(section, 0, section.length(), + mTmpBounds); + if (mIsRtl) { + int left = parent.getWidth() - mPaddingStart - mStartMargin; + c.drawText(section, left + (mStartMargin - mTmpBounds.width()) / 2, + child.getTop() + (2 * child.getPaddingTop()) + + mTmpBounds.height(), mSectionTextPaint); + } else { + int left = mPaddingStart; + c.drawText(section, left + (mStartMargin - mTmpBounds.width()) / 2, + child.getTop() + (2 * child.getPaddingTop()) + + mTmpBounds.height(), mSectionTextPaint); } } } @@ -112,6 +102,31 @@ class AppsGridAdapter extends RecyclerView.Adapter { RecyclerView.State state) { // Do nothing } + + private boolean shouldDrawItemSection(ViewHolder holder, View child, + List items) { + // Ensure item is not already removed + GridLayoutManager.LayoutParams lp = (GridLayoutManager.LayoutParams) + child.getLayoutParams(); + if (lp.isItemRemoved()) { + return false; + } + // Ensure we have a valid holder + if (holder == null) { + return false; + } + // Ensure it's not an empty row + if (holder.mIsEmptyRow) { + return false; + } + // Ensure we have a holder position + int pos = holder.getPosition(); + if (pos <= 0 || pos >= items.size()) { + return false; + } + // Only draw the first item in the section (the first one after the section header) + return items.get(pos - 1).isSectionHeader && !items.get(pos).isSectionHeader; + } } private LayoutInflater mLayoutInflater;