Skip to content

Commit

Permalink
Merge pull request #131 from Omega-R/feature/115_set_divider_drawable
Browse files Browse the repository at this point in the history
Feature/115 set divider drawable
  • Loading branch information
anton-knyazev authored May 20, 2019
2 parents 479f5f9 + 0c923a4 commit 2f84693
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 17 deletions.
2 changes: 1 addition & 1 deletion app/src/main/res/layout/fragment_test.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
android:divider="@drawable/divider"
android:dividerHeight="1dp"
app:itemSpace="32dp"
app:alphaDivider="0.5"
app:dividerAlpha="0.5"
app:dividerShow="middle"/>
</FrameLayout>
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ public class OmegaRecyclerView extends ExpandedRecyclerView implements SwipeMenu
private List<View> mHeadersList = new ArrayList<>();
private List<View> mFooterList = new ArrayList<>();
private WeakHashMap<ViewGroup.LayoutParams, SectionState> mLayoutParamCache = new WeakHashMap<>();
@Nullable
private DividerItemDecoration mDividerItemDecoration;
private int mItemSpace;

public OmegaRecyclerView(Context context) {
Expand All @@ -81,8 +83,8 @@ private void init(Context context, @Nullable AttributeSet attrs, int defStyleAtt
initDefaultLayoutManager(attrs, defStyleAttr);
if (attrs != null) {
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.OmegaRecyclerView, defStyleAttr, 0);
initDivider(a);
initItemSpace(a);
initDivider(a);
initEmptyView(a);
initPagination(a);
a.recycle();
Expand Down Expand Up @@ -123,31 +125,30 @@ public void initDivider(TypedArray a) {
}
}

float dividerHeight = a.getDimension(R.styleable.OmegaRecyclerView_dividerHeight,
int dividerHeight = (int) a.getDimension(R.styleable.OmegaRecyclerView_dividerHeight,
a.getDimension(R.styleable.OmegaRecyclerView_android_dividerHeight, -1));
float alpha = a.getFloat(R.styleable.OmegaRecyclerView_alphaDivider, 1);
int itemSpace = (int) a.getDimension(R.styleable.OmegaRecyclerView_itemSpace, 0);
float alpha = a.getFloat(R.styleable.OmegaRecyclerView_dividerAlpha, 1);

DividerItemDecoration decoration = new DividerItemDecoration(
mDividerItemDecoration = new DividerItemDecoration(
dividerDrawable,
(int) dividerHeight,
dividerHeight,
showDivider,
itemSpace / 2,
mItemSpace / 2,
alpha
);

int paddingStartDivider = a.getDimensionPixelSize(R.styleable.OmegaRecyclerView_dividerPaddingStart, 0);
int paddingEndDivider = a.getDimensionPixelSize(R.styleable.OmegaRecyclerView_dividerPaddingEnd, 0);

decoration.setPaddingStart(paddingStartDivider);
decoration.setPaddingEnd(paddingEndDivider);
mDividerItemDecoration.setPaddingStart(paddingStartDivider);
mDividerItemDecoration.setPaddingEnd(paddingEndDivider);

if (a.hasValue(R.styleable.OmegaRecyclerView_dividerPadding)) {
int paddingDivider = a.getDimensionPixelSize(R.styleable.OmegaRecyclerView_dividerPadding, 0);
decoration.setPadding(paddingDivider);
mDividerItemDecoration.setPadding(paddingDivider);
}

addItemDecoration(decoration);
addItemDecoration(mDividerItemDecoration);
}
}
}
Expand Down Expand Up @@ -416,12 +417,50 @@ public View transformTouchView(int touchPosition, View touchView) {
}

public void setDividerAlpha(float dividerAlpha) {
if (0 > dividerAlpha || dividerAlpha > 1) return;

if (mDividerItemDecoration != null) {
mDividerItemDecoration.setDividerAlpha(dividerAlpha);
}

boolean hasDividerDecoration = false;
for (int i = 0; i < getItemDecorationCount(); i++) {
RecyclerView.ItemDecoration itemDecoration = getItemDecorationAt(i);
if (itemDecoration instanceof DividerItemDecoration) {
hasDividerDecoration = true;
invalidateItemDecorations();
break;
}
}

if (!hasDividerDecoration && mDividerItemDecoration != null) {
addItemDecoration(mDividerItemDecoration);
}
}

public void setDividerDrawable(@Nullable Drawable drawable) {
if (mDividerItemDecoration != null && drawable != null) {
mDividerItemDecoration.setDividerDrawable(drawable);
}

boolean hasDividerDecoration = false;
for (int i = 0; i < getItemDecorationCount(); i++) {
RecyclerView.ItemDecoration itemDecoration = getItemDecorationAt(i);
if (itemDecoration instanceof DividerItemDecoration) {
((DividerItemDecoration) itemDecoration).setDividerAlpha(dividerAlpha);
hasDividerDecoration = true;
if (drawable == null) {
removeItemDecoration(itemDecoration);
break;
} else {
invalidateItemDecorations();
break;
}
}
}

if (drawable != null && !hasDividerDecoration) {
addItemDecoration(mDividerItemDecoration);
}
}

public void setPaginationCallback(OnPageRequestListener callback) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,21 @@
import com.omega_r.libs.omegarecyclerview.item_decoration.decoration_helpers.DividerDecorationHelper;

public class DividerItemDecoration extends BaseItemDecoration {
private final int mOffset;

private final Rect mViewRect = new Rect();
private final Rect mItemRect = new Rect();
private final int mOriginalDividerSize;
private final int mOffset;
private float mDividerAlpha;
private Drawable mDivider;
private int mDividerSize;
private int mPaddingStart;
private int mPaddingEnd;
private Rect mViewRect = new Rect();
private Rect mItemRect = new Rect();


public DividerItemDecoration(Drawable divider, int dividerSize, int showDivider, int offset, float dividerAlpha) {
super(showDivider);
mOriginalDividerSize = dividerSize;
mDivider = divider;
mDividerSize = dividerSize;
mOffset = offset;
Expand Down Expand Up @@ -55,6 +58,10 @@ public void setDividerAlpha(float dividerAlpha) {

public void setDividerDrawable(@NonNull Drawable dividerDrawable) {
mDivider = dividerDrawable;
if (mOriginalDividerSize < 0) {
mDividerSize = mOriginalDividerSize;
updateSize();
}
}

private void updateSize() {
Expand Down
2 changes: 1 addition & 1 deletion omegarecyclerviewlibs/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<flag name="middle" value="2" />
<flag name="end" value="4" />
</attr>
<attr name="alphaDivider" format="float" />
<attr name="dividerAlpha" format="float" />
<attr name="emptyView" format="reference" />
<attr name="paginationLayout" format="reference" />
<attr name="paginationErrorLayout" format="reference" />
Expand Down

0 comments on commit 2f84693

Please sign in to comment.