Skip to content

Commit

Permalink
Decorations are now measured properly
Browse files Browse the repository at this point in the history
Previously first/last flags were not taken into account and
offsets were cached sometimes in LayoutParams
  • Loading branch information
sergeys-opera committed Jun 22, 2015
1 parent b4a5cac commit fccd858
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 13 deletions.
7 changes: 7 additions & 0 deletions app/src/main/res/drawable/divider.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<size
android:width="30dp"
android:height="10dp"/>
<solid android:color="#ffd4d4d4"/>
</shape>
1 change: 1 addition & 0 deletions app/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<style name="ListItem">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_gravity">left|center_vertical</item>
<item name="android:orientation">horizontal</item>
<item name="android:background">@drawable/material_clickable_selector</item>
<item name="android:padding">5dp</item>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,24 @@
public class DividerItemDecoration extends RecyclerView.ItemDecoration {

private Drawable divider;
private int dividerHeight;
private int dividerWidth;
private boolean first = false;
private boolean last = false;

@SuppressWarnings("UnusedDeclaration")
public DividerItemDecoration(Context context, AttributeSet attrs) {
final TypedArray a = context.obtainStyledAttributes(attrs, new int[]{android.R.attr.listDivider});
divider = a.getDrawable(0);
setDivider(a.getDrawable(0));
a.recycle();
}

private void setDivider(Drawable divider) {
this.divider = divider;
this.dividerHeight = divider == null ? 0 : divider.getIntrinsicHeight();
this.dividerWidth = divider == null ? 0 : divider.getIntrinsicWidth();
}

@SuppressWarnings("UnusedDeclaration")
public DividerItemDecoration(Context context, AttributeSet attrs, boolean showFirstDivider,
boolean showLastDivider) {
Expand All @@ -33,7 +41,7 @@ public DividerItemDecoration(Context context, AttributeSet attrs, boolean showFi

@SuppressWarnings("UnusedDeclaration")
public DividerItemDecoration(Drawable divider) {
this.divider = divider;
setDivider(divider);
}

@SuppressWarnings("UnusedDeclaration")
Expand All @@ -47,24 +55,33 @@ public DividerItemDecoration(Drawable divider, boolean showFirstDivider,
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent,
RecyclerView.State state) {
super.getItemOffsets(outRect, view, parent, state);
if (divider == null) {
super.getItemOffsets(outRect, view, parent, state);
return;
}

final int position = ((RecyclerView.LayoutParams) view.getLayoutParams()).getViewLayoutPosition();
final boolean firstItem = position == 0;
final boolean lastItem = position == parent.getAdapter().getItemCount() - 1;
final boolean dividerBefore = first || !firstItem;
if (!dividerBefore) {
super.getItemOffsets(outRect, view, parent, state);
return;
}

if (getOrientation(parent) == LinearLayoutManager.VERTICAL) {
outRect.top = divider.getIntrinsicHeight();
outRect.bottom = -outRect.top;
outRect.top = dividerHeight;
outRect.bottom = last && lastItem ? dividerHeight : 0;
} else {
outRect.left = divider.getIntrinsicWidth();
outRect.right = -outRect.left;
outRect.left = dividerWidth;
outRect.right = last && lastItem ? dividerWidth : 0;
}
}

@Override
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
if (divider == null) {
super.onDrawOver(c, parent, state);
super.onDraw(c, parent, state);
return;
}

Expand All @@ -79,11 +96,11 @@ public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state)
final boolean vertical = orientation == LinearLayoutManager.VERTICAL;
final int size;
if (vertical) {
size = divider.getIntrinsicHeight();
size = dividerHeight;
left = parent.getPaddingLeft();
right = parent.getWidth() - parent.getPaddingRight();
} else {
size = divider.getIntrinsicWidth();
size = dividerWidth;
top = parent.getPaddingTop();
bottom = parent.getHeight() - parent.getPaddingBottom();
}
Expand All @@ -92,10 +109,10 @@ public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state)
final View child = parent.getChildAt(i);
final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
if (vertical) {
top = child.getTop() - params.topMargin;
top = child.getTop() - params.topMargin - size;
bottom = top + size;
} else {
left = child.getLeft() - params.leftMargin;
left = child.getLeft() - params.leftMargin - size;
right = left + size;
}
divider.setBounds(left, top, right, bottom);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package org.solovyev.android.views.llm;

import android.content.Context;
import android.graphics.Rect;
import android.support.v4.view.ViewCompat;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import java.lang.reflect.Field;

/**
* {@link android.support.v7.widget.LinearLayoutManager} which wraps its content. Note that this class will always
* wrap the content regardless of {@link android.support.v7.widget.RecyclerView} layout parameters.
Expand All @@ -18,6 +21,9 @@
*/
public class LinearLayoutManager extends android.support.v7.widget.LinearLayoutManager {

private static boolean canMakeInsetsDirty = true;
private static Field insetsDirtyField = null;

private static final int CHILD_WIDTH = 0;
private static final int CHILD_HEIGHT = 1;
private static final int DEFAULT_CHILD_SIZE = 100;
Expand All @@ -28,6 +34,7 @@ public class LinearLayoutManager extends android.support.v7.widget.LinearLayoutM
private int childSize = DEFAULT_CHILD_SIZE;
private boolean hasChildSize;
private int overScrollMode = ViewCompat.OVER_SCROLL_ALWAYS;
private final Rect tmpRect = new Rect();

@SuppressWarnings("UnusedDeclaration")
public LinearLayoutManager(Context context) {
Expand Down Expand Up @@ -227,6 +234,11 @@ private void measureChild(RecyclerView.Recycler recycler, int position, int widt
final int hMargin = p.leftMargin + p.rightMargin;
final int vMargin = p.topMargin + p.bottomMargin;

// we must make insets dirty in order calculateItemDecorationsForChild to work
makeInsetsDirty(p);
// this method should be called before any getXxxDecorationXxx() methods
calculateItemDecorationsForChild(child, tmpRect);

final int hDecoration = getRightDecorationWidth(child) + getLeftDecorationWidth(child);
final int vDecoration = getTopDecorationHeight(child) + getBottomDecorationHeight(child);

Expand All @@ -238,6 +250,32 @@ private void measureChild(RecyclerView.Recycler recycler, int position, int widt
dimensions[CHILD_WIDTH] = getDecoratedMeasuredWidth(child) + p.leftMargin + p.rightMargin;
dimensions[CHILD_HEIGHT] = getDecoratedMeasuredHeight(child) + p.bottomMargin + p.topMargin;

// as view is recycled let's not keep old measured values
makeInsetsDirty(p);
recycler.recycleView(child);
}

private static void makeInsetsDirty(RecyclerView.LayoutParams p) {
if (!canMakeInsetsDirty) {
return;
}
try {
if (insetsDirtyField == null) {
insetsDirtyField = RecyclerView.LayoutParams.class.getDeclaredField("mInsetsDirty");
insetsDirtyField.setAccessible(true);
}
insetsDirtyField.set(p, true);
} catch (NoSuchFieldException e) {
onMakeInsertDirtyFailed();
} catch (IllegalAccessException e) {
onMakeInsertDirtyFailed();
}
}

private static void onMakeInsertDirtyFailed() {
canMakeInsetsDirty = false;
if (BuildConfig.DEBUG) {
Log.w("LinearLayoutManager", "Can't make LayoutParams insets dirty, decorations measurements might be incorrect");
}
}
}

0 comments on commit fccd858

Please sign in to comment.