Skip to content

Commit

Permalink
Merge pull request #1 from yqritc/feat/develop
Browse files Browse the repository at this point in the history
Feat/develop
  • Loading branch information
yqritc committed Jan 22, 2015
2 parents 22e3689 + 5eb92fc commit 7be1bd9
Show file tree
Hide file tree
Showing 13 changed files with 257 additions and 53 deletions.
28 changes: 22 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,20 @@ repositories {
}
dependencies {
compile 'com.yqritc:recyclerview-flexibledivider:1.0.0'
compile 'com.yqritc:recyclerview-flexibledivider:1.0.1'
}
```

# Usage

The following is the simplest usage.
Drawing a divider drawable retrived from android.R.attr.listDivider between each cell.
Drawing a divider drawable retrieved from android.R.attr.listDivider between each cell.
```
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
recyclerView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(this))
```

If you want to set color, size and margin values
You can specify as the followings.
If you want to set color, size and margin values, you can specify as the followings.
```
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
recyclerView.addItemDecoration(
Expand All @@ -38,7 +37,19 @@ recyclerView.addItemDecoration(
.build());
```

If you want to cusomize divider depending on the position, implement the following interfaces.

Instead of setting color and size, you can set paint object.
```
Paint paint = new Paint();
paint.setStrokeWidth(5);
paint.setColor(Color.BLUE);
paint.setAntiAlias(true);
paint.setPathEffect(new DashPathEffect(new float[]{25.0f, 25.0f}, 0));
recyclerView.addItemDecoration(
new HorizontalDividerItemDecoration.Builder(this).paint(paint).build());
```

If you want to customize divider depending on the position, implement the following interfaces.

### List of provider
The following providers can be implemented and controllable for each divider drawn between cells.
Expand All @@ -49,15 +60,20 @@ If non of color is specified, default divider retrieved from android.R.attr.list
- SizeProvider
Provide height for horizontal divider, width for vertical divider.

- PaintProvider
Provide paint object for divider line to draw.

- VisibilityProvider
Enables you to control the visibiity of dividers.
Enables you to control the visibility of dividers.

- MarginProvider for horizontal divider (vertical list)
Enables you to specify left and right margin of divider.

- MarginProvider for vertical divider (horizontal list)
Enables you to specify top and bottom margin of divider.

### Caution
- When you set Paint, you must use setColor and setStrokeWidth methods of paint class.

# License
```
Expand Down
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
VERSION_NAME=1.0.0
VERSION_CODE=1
VERSION_NAME=1.0.1
VERSION_CODE=2
GROUP=com.yqritc
ARTIFACT_ID=recyclerview-flexibledivider

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,30 +19,45 @@ public abstract class FlexibleDividerDecoration extends RecyclerView.ItemDecorat
android.R.attr.listDivider
};

protected boolean mIsPaintMode;
protected enum DividerType {
DRAWABLE, PAINT, COLOR
}

protected DividerType mDividerType = DividerType.DRAWABLE;
protected Drawable mDivider;
protected Paint mPaint;
protected VisibilityProvider mVisibilityProvider;
protected PaintProvider mPaintProvider;
protected ColorProvider mColorProvider;
protected SizeProvider mSizeProvider;
private Paint mPaint;

protected FlexibleDividerDecoration(Builder builder) {
mColorProvider = builder.mColorProvider;
mIsPaintMode = mColorProvider != null;
if (mIsPaintMode) {
if (builder.mPaintProvider != null) {
mDividerType = DividerType.PAINT;
mPaintProvider = builder.mPaintProvider;
} else if (builder.mColorProvider != null) {
mDividerType = DividerType.COLOR;
mColorProvider = builder.mColorProvider;
mPaint = new Paint();
setSizeProvider(builder);
} else {
mDividerType = DividerType.DRAWABLE;
TypedArray a = builder.mContext.obtainStyledAttributes(ATTRS);
mDivider = a.getDrawable(0);
a.recycle();
setSizeProvider(builder);
}

mVisibilityProvider = builder.mVisibilityProvider;
}

private void setSizeProvider(Builder builder) {
mSizeProvider = builder.mSizeProvider;
if (mSizeProvider == null) {
mSizeProvider = new SizeProvider() {
@Override
public int dividerSize(int position, RecyclerView parent) {
if (mIsPaintMode) {
if (mDividerType == DividerType.COLOR) {
return DEFAULT_SIZE;
} else {
return getDividerSize();
Expand All @@ -51,8 +66,6 @@ public int dividerSize(int position, RecyclerView parent) {
};

}

mVisibilityProvider = builder.mVisibilityProvider;
}

@Override
Expand All @@ -66,13 +79,20 @@ public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state)
}

Rect bounds = getDividerBound(childPosition, parent, child);
if (mIsPaintMode) {
mPaint.setColor(mColorProvider.dividerColor(childPosition, parent));
mPaint.setStrokeWidth(mSizeProvider.dividerSize(childPosition, parent));
c.drawLine(bounds.left, bounds.top, bounds.right, bounds.bottom, mPaint);
} else {
mDivider.setBounds(bounds);
mDivider.draw(c);
switch (mDividerType) {
case DRAWABLE:
mDivider.setBounds(bounds);
mDivider.draw(c);
break;
case PAINT:
mPaint = mPaintProvider.dividerPaint(childPosition, parent);
c.drawLine(bounds.left, bounds.top, bounds.right, bounds.bottom, mPaint);
break;
case COLOR:
mPaint.setColor(mColorProvider.dividerColor(childPosition, parent));
mPaint.setStrokeWidth(mSizeProvider.dividerSize(childPosition, parent));
c.drawLine(bounds.left, bounds.top, bounds.right, bounds.bottom, mPaint);
break;
}
}
}
Expand All @@ -81,7 +101,7 @@ public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state)
public void getItemOffsets(Rect outRect, View view, RecyclerView parent,
RecyclerView.State state) {
super.getItemOffsets(outRect, view, parent, state);
if (mIsPaintMode) {
if (mDividerType != DividerType.DRAWABLE) {
return;
}
getDividerOffset(outRect);
Expand All @@ -105,6 +125,11 @@ public interface VisibilityProvider {
public boolean shouldHideDivider(int position, RecyclerView parent);
}

public interface PaintProvider {

public Paint dividerPaint(int position, RecyclerView parent);
}

public interface ColorProvider {

/**
Expand Down Expand Up @@ -133,6 +158,7 @@ public interface SizeProvider {
public static class Builder<T extends Builder> {

private Context mContext;
private PaintProvider mPaintProvider;
private ColorProvider mColorProvider;
private SizeProvider mSizeProvider;
private VisibilityProvider mVisibilityProvider = new VisibilityProvider() {
Expand All @@ -146,6 +172,20 @@ public Builder(Context context) {
mContext = context;
}

public T paint(final Paint paint) {
return paintProvider(new PaintProvider() {
@Override
public Paint dividerPaint(int position, RecyclerView parent) {
return paint;
}
});
}

public T paintProvider(PaintProvider provider) {
mPaintProvider = provider;
return (T) this;
}

public T color(final int color) {
return colorProvider(new ColorProvider() {
@Override
Expand Down Expand Up @@ -178,5 +218,18 @@ public T visibilityProvider(VisibilityProvider provider) {
mVisibilityProvider = provider;
return (T) this;
}

protected void checkBuilderParams() {
if (mPaintProvider != null) {
if (mColorProvider != null) {
throw new IllegalArgumentException(
"Use setColor method of Paint class to specify line color. Do not provider ColorProvider if you set PaintProvider.");
}
if (mSizeProvider != null) {
throw new IllegalArgumentException(
"Use setStrokeWidth method of Paint class to specify line size. Do not provider SizeProvider if you set PaintProvider.");
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,16 @@ protected Rect getDividerBound(int position, RecyclerView parent, View child) {
mMarginProvider.dividerLeftMargin(position, parent);
bounds.right = parent.getWidth() - parent.getPaddingRight() -
mMarginProvider.dividerRightMargin(position, parent);
if (mIsPaintMode) {
bounds.top = child.getBottom() + params.topMargin;
bounds.bottom = bounds.top;
} else {

if (mDividerType == DividerType.DRAWABLE) {
int dividerSize = mSizeProvider.dividerSize(position, parent);
bounds.top = child.getBottom() + params.topMargin - dividerSize / 2;
bounds.bottom = bounds.top + dividerSize;
} else {
bounds.top = child.getBottom() + params.topMargin;
bounds.bottom = bounds.top;
}

return bounds;
}

Expand Down Expand Up @@ -106,6 +108,7 @@ public Builder marginProvider(MarginProvider provider) {
}

public HorizontalDividerItemDecoration build() {
checkBuilderParams();
return new HorizontalDividerItemDecoration(this);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,16 @@ protected Rect getDividerBound(int position, RecyclerView parent, View child) {
mMarginProvider.dividerTopMargin(position, parent);
bounds.bottom = parent.getHeight() - parent.getPaddingBottom() -
mMarginProvider.dividerBottomMargin(position, parent);
if (mIsPaintMode) {
bounds.left = child.getRight() + params.leftMargin;
bounds.right = bounds.left;
} else {

if (mDividerType == DividerType.DRAWABLE) {
int dividerSize = mSizeProvider.dividerSize(position, parent);
bounds.left = child.getRight() + params.leftMargin - dividerSize / 2;
bounds.right = bounds.left + dividerSize;
} else {
bounds.left = child.getRight() + params.leftMargin;
bounds.right = bounds.left;
}

return bounds;
}

Expand Down Expand Up @@ -105,6 +107,7 @@ public Builder marginProvider(MarginProvider provider) {
}

public VerticalDividerItemDecoration build() {
checkBuilderParams();
return new VerticalDividerItemDecoration(this);
}
}
Expand Down
Binary file modified sample/sample2.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions sample/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity
android:name=".PaintActivity"
android:label="@string/menu_paint"/>
<activity
android:name=".ComplexActivity"
android:label="@string/menu_complex"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import android.support.v7.widget.RecyclerView;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;


public class ComplexActivity extends ActionBarActivity {
Expand All @@ -26,15 +27,20 @@ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sample);

RecyclerView recyclerView = (RecyclerView) findViewById(R.id.main_recyclerview);
// Workaround for dash path effect
// https://code.google.com/p/android/issues/detail?id=29944
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) {
recyclerView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}

ComplexAdapter adapter = new ComplexAdapter(this);
LinearLayoutManager manager = new LinearLayoutManager(this);
manager.setOrientation(OrientationHelper.VERTICAL);
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.main_recyclerview);
recyclerView.setLayoutManager(manager);
recyclerView.setAdapter(adapter);
recyclerView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(this)
.sizeProvider(adapter)
.colorProvider(adapter)
.paintProvider(adapter)
.visibilityProvider(adapter)
.marginProvider(adapter)
.build());
Expand All @@ -53,6 +59,9 @@ public boolean onOptionsItemSelected(MenuItem item) {
case R.id.action_simple:
SimpleActivity.startActivity(this);
return true;
case R.id.action_paint:
PaintActivity.startActivity(this);
return true;
case R.id.action_complex:
ComplexActivity.startActivity(this);
return true;
Expand Down
Loading

0 comments on commit 7be1bd9

Please sign in to comment.