Skip to content

Commit

Permalink
Switched grid dividers from item background to RV decoration. No more…
Browse files Browse the repository at this point in the history
… 9-patches!
  • Loading branch information
nickbutcher committed Nov 24, 2015
1 parent eff9819 commit 857c311
Show file tree
Hide file tree
Showing 10 changed files with 112 additions and 125 deletions.
10 changes: 9 additions & 1 deletion app/src/main/java/io/plaidapp/ui/FeedAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,8 @@ public void onClick(final View view) {
final int translatedLeft =
(holder.itemView.getWidth() - pocketButton.getWidth()) / 2;
final int translatedTop =
initialTop - ((holder.itemView.getHeight() - pocketButton.getHeight()) / 2);
initialTop - ((holder.itemView.getHeight() - pocketButton.getHeight()
) / 2);
final ArcMotion arc = new ArcMotion();

// animate the title & pocket icon up, scale the pocket icon up
Expand Down Expand Up @@ -585,6 +586,13 @@ public int getDataItemCount() {
return items.size();
}

/**
* Which ViewHolder types require a divider decoration
*/
public Class[] getDividedViewHolderClasses() {
return new Class[] { DesignerNewsStoryHolder.class, ProductHuntStoryHolder.class };
}

/* protected */ class DribbbleShotHolder extends RecyclerView.ViewHolder {

public DribbbleShotHolder(View itemView) {
Expand Down
10 changes: 7 additions & 3 deletions app/src/main/java/io/plaidapp/ui/HomeActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
import io.plaidapp.data.prefs.DribbblePrefs;
import io.plaidapp.data.prefs.SourceManager;
import io.plaidapp.ui.recyclerview.FilterTouchHelperCallback;
import io.plaidapp.ui.recyclerview.GridItemDividerDecoration;
import io.plaidapp.ui.recyclerview.InfiniteScrollListener;
import io.plaidapp.ui.transitions.FabDialogMorphSetup;
import io.plaidapp.util.ViewUtils;
Expand Down Expand Up @@ -174,6 +175,8 @@ public void onLoadMore() {
}
});
grid.setHasFixedSize(true);
grid.addItemDecoration(new GridItemDividerDecoration(adapter.getDividedViewHolderClasses(),
this, R.dimen.divider_height, R.color.divider));

// drawer layout treats fitsSystemWindows specially so we have to handle insets ourselves
drawer.setOnApplyWindowInsetsListener(new View.OnApplyWindowInsetsListener() {
Expand Down Expand Up @@ -212,9 +215,10 @@ public WindowInsets onApplyWindowInsets(View v, WindowInsets insets) {
// need to set the padding end for landscape case
final boolean ltr = filtersList.getLayoutDirection() == View.LAYOUT_DIRECTION_LTR;
filtersList.setPaddingRelative(filtersList.getPaddingStart(),
filtersList.getPaddingTop() + insets.getSystemWindowInsetTop(),
filtersList.getPaddingEnd() + (ltr ? insets.getSystemWindowInsetRight() : 0),
filtersList.getPaddingBottom() + insets.getSystemWindowInsetBottom());
filtersList.getPaddingTop() + insets.getSystemWindowInsetTop(),
filtersList.getPaddingEnd() + (ltr ? insets.getSystemWindowInsetRight() :
0),
filtersList.getPaddingBottom() + insets.getSystemWindowInsetBottom());

// clear this listener so insets aren't re-applied
drawer.setOnApplyWindowInsetsListener(null);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* Copyright 2015 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.plaidapp.ui.recyclerview;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.support.annotation.ColorInt;
import android.support.annotation.ColorRes;
import android.support.annotation.DimenRes;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.View;

/**
* A {@link RecyclerView.ItemDecoration} which draws dividers (along the right & bottom)
* for certain {@link RecyclerView.ViewHolder} types.
*/
public class GridItemDividerDecoration extends RecyclerView.ItemDecoration {

private final Class[] dividedClasses;
private final float dividerSize;
private final Paint paint;

public GridItemDividerDecoration(Class[] dividedClasses,
float dividerSize,
@ColorInt int dividerColor) {
this.dividedClasses = dividedClasses;
this.dividerSize = dividerSize;
paint = new Paint();
paint.setColor(dividerColor);
paint.setStyle(Paint.Style.FILL);
}

public GridItemDividerDecoration(Class[] dividedClasses,
@NonNull Context context,
@DimenRes int dividerSizeResId,
@ColorRes int dividerColorResId) {
this(dividedClasses,
context.getResources().getDimensionPixelSize(dividerSizeResId),
ContextCompat.getColor(context, dividerColorResId));
}

@Override
public void onDrawOver(Canvas canvas, RecyclerView parent, RecyclerView.State state) {
final int childCount = parent.getChildCount();
final RecyclerView.LayoutManager lm = parent.getLayoutManager();
for (int i = 0; i < childCount; i++) {
final View child = parent.getChildAt(i);
RecyclerView.ViewHolder viewHolder = parent.getChildViewHolder(child);

if (requiresDivider(viewHolder)) {
final int right = lm.getDecoratedRight(child);
final int bottom = lm.getDecoratedBottom(child);
// draw the bottom divider
canvas.drawRect(lm.getDecoratedLeft(child),
bottom - dividerSize,
right,
bottom,
paint);
// draw the right edge divider
canvas.drawRect(right - dividerSize,
lm.getDecoratedTop(child),
right,
bottom - dividerSize,
paint);
}

}
}

private boolean requiresDivider(RecyclerView.ViewHolder viewHolder) {
for (int i = 0; i < dividedClasses.length; i++) {
if (dividedClasses[i].isInstance(viewHolder)) return true;
}
return false;
}

}
Binary file not shown.
36 changes: 0 additions & 36 deletions app/src/main/res/drawable-v23/designer_news_item_background.xml

This file was deleted.

36 changes: 0 additions & 36 deletions app/src/main/res/drawable-v23/product_hunt_item_background.xml

This file was deleted.

25 changes: 0 additions & 25 deletions app/src/main/res/drawable/designer_news_item_background.xml

This file was deleted.

22 changes: 0 additions & 22 deletions app/src/main/res/drawable/product_hunt_item_background.xml

This file was deleted.

2 changes: 1 addition & 1 deletion app/src/main/res/layout/designer_news_story_item.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
android:id="@+id/story_title_background"
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="@drawable/designer_news_item_background"
android:background="@color/designer_news"
android:foreground="@drawable/light_ripple"
android:orientation="vertical"
android:stateListAnimator="@animator/raise"
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/product_hunt_item.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/product_hunt_item_background"
android:background="@color/product_hunt"
android:foreground="@drawable/dark_ripple"
android:orientation="vertical"
android:stateListAnimator="@animator/raise">
Expand Down

0 comments on commit 857c311

Please sign in to comment.