Skip to content

Commit 888956a

Browse files
author
roman_tcaregorodtcev
committed
setDividerDrawable method has been added
1 parent 0670d59 commit 888956a

File tree

1 file changed

+36
-12
lines changed

1 file changed

+36
-12
lines changed

omegarecyclerviewlibs/src/main/java/com/omega_r/libs/omegarecyclerview/OmegaRecyclerView.java

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ public class OmegaRecyclerView extends ExpandedRecyclerView implements SwipeMenu
5959
private List<View> mHeadersList = new ArrayList<>();
6060
private List<View> mFooterList = new ArrayList<>();
6161
private WeakHashMap<ViewGroup.LayoutParams, SectionState> mLayoutParamCache = new WeakHashMap<>();
62+
@Nullable
63+
private DividerItemDecoration mDividerItemDecoration;
6264
private int mItemSpace;
6365

6466
public OmegaRecyclerView(Context context) {
@@ -81,8 +83,8 @@ private void init(Context context, @Nullable AttributeSet attrs, int defStyleAtt
8183
initDefaultLayoutManager(attrs, defStyleAttr);
8284
if (attrs != null) {
8385
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.OmegaRecyclerView, defStyleAttr, 0);
84-
initDivider(a);
8586
initItemSpace(a);
87+
initDivider(a);
8688
initEmptyView(a);
8789
initPagination(a);
8890
a.recycle();
@@ -123,31 +125,30 @@ public void initDivider(TypedArray a) {
123125
}
124126
}
125127

126-
float dividerHeight = a.getDimension(R.styleable.OmegaRecyclerView_dividerHeight,
128+
int dividerHeight = (int) a.getDimension(R.styleable.OmegaRecyclerView_dividerHeight,
127129
a.getDimension(R.styleable.OmegaRecyclerView_android_dividerHeight, -1));
128130
float alpha = a.getFloat(R.styleable.OmegaRecyclerView_alphaDivider, 1);
129-
int itemSpace = (int) a.getDimension(R.styleable.OmegaRecyclerView_itemSpace, 0);
130131

131-
DividerItemDecoration decoration = new DividerItemDecoration(
132+
mDividerItemDecoration = new DividerItemDecoration(
132133
dividerDrawable,
133-
(int) dividerHeight,
134+
dividerHeight,
134135
showDivider,
135-
itemSpace / 2,
136+
mItemSpace / 2,
136137
alpha
137138
);
138139

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

142-
decoration.setPaddingStart(paddingStartDivider);
143-
decoration.setPaddingEnd(paddingEndDivider);
143+
mDividerItemDecoration.setPaddingStart(paddingStartDivider);
144+
mDividerItemDecoration.setPaddingEnd(paddingEndDivider);
144145

145146
if (a.hasValue(R.styleable.OmegaRecyclerView_dividerPadding)) {
146147
int paddingDivider = a.getDimensionPixelSize(R.styleable.OmegaRecyclerView_dividerPadding, 0);
147-
decoration.setPadding(paddingDivider);
148+
mDividerItemDecoration.setPadding(paddingDivider);
148149
}
149150

150-
addItemDecoration(decoration);
151+
addItemDecoration(mDividerItemDecoration);
151152
}
152153
}
153154
}
@@ -416,27 +417,50 @@ public View transformTouchView(int touchPosition, View touchView) {
416417
}
417418

418419
public void setDividerAlpha(float dividerAlpha) {
420+
if (0 > dividerAlpha && dividerAlpha > 1) return;
421+
422+
if (mDividerItemDecoration != null) {
423+
mDividerItemDecoration.setDividerAlpha(dividerAlpha);
424+
}
425+
426+
boolean hasDividerDecoration = false;
419427
for (int i = 0; i < getItemDecorationCount(); i++) {
420428
RecyclerView.ItemDecoration itemDecoration = getItemDecorationAt(i);
421429
if (itemDecoration instanceof DividerItemDecoration) {
422-
((DividerItemDecoration) itemDecoration).setDividerAlpha(dividerAlpha);
430+
hasDividerDecoration = true;
431+
invalidateItemDecorations();
432+
break;
423433
}
424434
}
435+
436+
if (!hasDividerDecoration && mDividerItemDecoration != null) {
437+
addItemDecoration(mDividerItemDecoration);
438+
}
425439
}
426440

427441
public void setDividerDrawable(@Nullable Drawable drawable) {
442+
if (mDividerItemDecoration != null && drawable != null) {
443+
mDividerItemDecoration.setDividerDrawable(drawable);
444+
}
445+
446+
boolean hasDividerDecoration = false;
428447
for (int i = 0; i < getItemDecorationCount(); i++) {
429448
RecyclerView.ItemDecoration itemDecoration = getItemDecorationAt(i);
430449
if (itemDecoration instanceof DividerItemDecoration) {
450+
hasDividerDecoration = true;
431451
if (drawable == null) {
432452
removeItemDecoration(itemDecoration);
433453
break;
434454
} else {
435-
((DividerItemDecoration) itemDecoration).setDividerDrawable(drawable);
436455
invalidateItemDecorations();
456+
break;
437457
}
438458
}
439459
}
460+
461+
if (drawable != null && !hasDividerDecoration) {
462+
addItemDecoration(mDividerItemDecoration);
463+
}
440464
}
441465

442466
public void setPaginationCallback(OnPageRequestListener callback) {

0 commit comments

Comments
 (0)