Skip to content

Commit

Permalink
Merge pull request #162 from Omega-R/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
anton-knyazev authored Dec 22, 2022
2 parents 6d504af + 2d4dec7 commit 980b637
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@
import androidx.annotation.StringRes;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.ExpandedRecyclerView;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.omega_r.libs.omegarecyclerview.header.HeaderFooterWrapperAdapter;
import com.omega_r.libs.omegarecyclerview.item_decoration.DividerItemDecoration;
import com.omega_r.libs.omegarecyclerview.header.SectionSize;
import com.omega_r.libs.omegarecyclerview.item_decoration.BaseSpaceItemDecoration;
import com.omega_r.libs.omegarecyclerview.item_decoration.DividerItemDecoration;
import com.omega_r.libs.omegarecyclerview.pagination.OnPageRequestListener;
import com.omega_r.libs.omegarecyclerview.pagination.PageRequester;
import com.omega_r.libs.omegarecyclerview.pagination.PaginationAdapter;
Expand All @@ -46,6 +48,8 @@
public class OmegaRecyclerView extends ExpandedRecyclerView implements SwipeMenuHelper.Callback {

private static final int[] DEFAULT_DIVIDER_ATTRS = new int[]{android.R.attr.listDivider};
private static final int HEADER = 0;
private static final int FOOTER = 1;

private final WeakHashMap<ViewGroup.LayoutParams, SectionState> mLayoutParamCache = new WeakHashMap<>();
private final SwipeMenuHelper mSwipeMenuHelper = new SwipeMenuHelper(this);
Expand All @@ -70,6 +74,10 @@ public class OmegaRecyclerView extends ExpandedRecyclerView implements SwipeMenu
private int mItemSpace;
private int mDividerSize;
private boolean mIsAdapterConnected;
private int mHeaderType = SectionSize.DEFAULT.getValue();
private int mFooterType = SectionSize.DEFAULT.getValue();
private int mHeaderSpanSize;
private int mFooterSpanSize;

public OmegaRecyclerView(Context context) {
super(context);
Expand Down Expand Up @@ -118,7 +126,8 @@ private void initItemSpace(TypedArray a) {
mItemSpace = (int) a.getDimension(R.styleable.OmegaRecyclerView_itemSpace, 0);
mBaseSpaceItemDecoration = createSpaceItemDecoration(showDivider, mItemSpace);

if (a.hasValue(R.styleable.OmegaRecyclerView_itemSpace)) addItemDecoration(mBaseSpaceItemDecoration);
if (a.hasValue(R.styleable.OmegaRecyclerView_itemSpace))
addItemDecoration(mBaseSpaceItemDecoration);
}

protected BaseSpaceItemDecoration createSpaceItemDecoration(int showDivider, int itemSpace) {
Expand Down Expand Up @@ -180,6 +189,59 @@ private void initStickyMode(TypedArray a) {
mStickyMode = a.getInt(R.styleable.OmegaRecyclerView_stickyMode, mStickyMode);
}

private void initHeaderAndFooter(int section, TypedArray a) {
if (section == HEADER) {
if (a.hasValue(R.styleable.OmegaRecyclerView_Layout_sectionType)) {
mHeaderType = a.getInt(R.styleable.OmegaRecyclerView_Layout_sectionType, mHeaderType);
}
if (a.hasValue(R.styleable.OmegaRecyclerView_Layout_spanSize)) {
mHeaderSpanSize = a.getInt(R.styleable.OmegaRecyclerView_Layout_spanSize, 0);
}
} else if (section == FOOTER) {
if (a.hasValue(R.styleable.OmegaRecyclerView_Layout_sectionType)) {
mFooterType = a.getInt(R.styleable.OmegaRecyclerView_Layout_sectionType, mHeaderType);
}
if (a.hasValue(R.styleable.OmegaRecyclerView_Layout_spanSize)) {
mFooterSpanSize = a.getInt(R.styleable.OmegaRecyclerView_Layout_spanSize, 0);
}
}
}

private void checkLayoutManager(LayoutManager layout, RecyclerView.Adapter adapter) {
if (layout instanceof GridLayoutManager && adapter instanceof HeaderFooterWrapperAdapter) {
setFooterAndHeaderSpan(
(GridLayoutManager) layout,
(HeaderFooterWrapperAdapter) adapter
);
}
}

private void setFooterAndHeaderSpan(final GridLayoutManager lm, final HeaderFooterWrapperAdapter adapter) {
lm.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
if (adapter.isHeadersVisible() && adapter.isHeader(adapter.getItemViewType(position))) {
return getItemSpan(lm, mHeaderType, mHeaderSpanSize);
}
if (adapter.isFootersVisible() && adapter.isFooter(adapter.getItemViewType(position))) {
return getItemSpan(lm, mFooterType, mFooterSpanSize);
}
return 1;
}
});
}

private int getItemSpan(GridLayoutManager lm, int spanType, int span) {
int maxSpan = lm.getSpanCount();
if (spanType == SectionSize.FULL.getValue()) {
return maxSpan;
}
if (spanType == SectionSize.CUSTOM.getValue() && span > 0) {
return Math.min(span, maxSpan);
}
return 1;
}

@Override
@SuppressWarnings("unchecked")
public void setAdapter(RecyclerView.Adapter adapter) {
Expand All @@ -205,6 +267,7 @@ public void setAdapter(RecyclerView.Adapter adapter) {
shellAdapter = new HeaderFooterWrapperAdapter(shellAdapter);
((HeaderFooterWrapperAdapter) shellAdapter).setHeaders(mHeadersList);
((HeaderFooterWrapperAdapter) shellAdapter).setFooters(mFooterList);
checkLayoutManager(getLayoutManager(), shellAdapter);
}
}
super.setAdapter(shellAdapter);
Expand Down Expand Up @@ -304,6 +367,7 @@ public void smoothScrollToRealPosition(int position) {
}
super.smoothScrollToPosition(scrollPosition);
}

@NonNull
protected final BaseSpaceItemDecoration getSpaceDecoration() {
return mBaseSpaceItemDecoration;
Expand Down Expand Up @@ -406,6 +470,7 @@ public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
final TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.OmegaRecyclerView_Layout);
int section = typedArray.getInt(R.styleable.OmegaRecyclerView_Layout_layout_section, 0);
boolean showDivider = typedArray.getBoolean(R.styleable.OmegaRecyclerView_Layout_layout_showDivider, true);
initHeaderAndFooter(section, typedArray);
typedArray.recycle();
mLayoutParamCache.put(layoutParams, new SectionState(section, showDivider));
}
Expand All @@ -432,7 +497,8 @@ private void findEmptyView() {
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
boolean result = mSwipeMenuHelper.handleInterceptTouchEvent(ev, super.onInterceptTouchEvent(ev));
if (mBaseStickyDecoration != null && !result) return mBaseStickyDecoration.onTouchEvent(this, ev, result);
if (mBaseStickyDecoration != null && !result)
return mBaseStickyDecoration.onTouchEvent(this, ev, result);
return result;
}

Expand Down Expand Up @@ -602,6 +668,40 @@ public void setFootersVisibility(boolean visible) {
}
}

public void setHeaderSpanSize(int spanSize) {
mHeaderSpanSize = spanSize;
checkLayoutManager(getLayoutManager(), getAdapter());
}

public int getHeaderSpanSize() {
return mHeaderSpanSize;
}

public void setFooterSpanSize(int spanSize) {
mFooterSpanSize = spanSize;
checkLayoutManager(getLayoutManager(), getAdapter());
}

public int getFooterSpanSize() {
return mFooterSpanSize;
}

public void setHeaderType(SectionSize sizeType) {
mHeaderType = sizeType.getValue();
}

public SectionSize getHeaderType() {
return SectionSize.valueOf(mHeaderType);
}

public void setFooterType(SectionSize sizeType) {
mFooterType = sizeType.getValue();
}

public SectionSize getFooterType() {
return SectionSize.valueOf(mFooterType);
}

private final AdapterDataObserver mEmptyObserver = new AdapterDataObserver() {
@Override
public void onChanged() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package com.omega_r.libs.omegarecyclerview.header;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;
import android.util.SparseArray;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import com.omega_r.libs.omegarecyclerview.OmegaRecyclerView;
import com.omega_r.libs.omegarecyclerview.R;
import com.omega_r.libs.omegarecyclerview.sticky_decoration.StickyAdapter;
import com.omega_r.libs.omegarecyclerview.sticky_decoration.HeaderStickyDecoration;
import com.omega_r.libs.omegarecyclerview.sticky_decoration.StickyAdapter;

import java.util.List;

Expand Down Expand Up @@ -137,11 +138,11 @@ public T getWrappedAdapter() {
return mRealAdapter;
}

private boolean isHeader(int viewType) {
public boolean isHeader(int viewType) {
return viewType >= BASE_HEADER_VIEW_TYPE && viewType < (BASE_HEADER_VIEW_TYPE + mHeaderArray.size());
}

private boolean isFooter(int viewType) {
public boolean isFooter(int viewType) {
return viewType >= BASE_FOOTER_VIEW_TYPE && viewType < (BASE_FOOTER_VIEW_TYPE + mFooterArray.size());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.omega_r.libs.omegarecyclerview.header;

import java.util.HashMap;
import java.util.Map;

public enum SectionSize {

CUSTOM(-1),
FULL(0),
DEFAULT(1);

private final int value;
private static final Map<Integer, SectionSize> map = new HashMap<>();

SectionSize(int value) {
this.value = value;
}

static {
for (SectionSize sectionSize: SectionSize.values()) {
map.put(sectionSize.value, sectionSize);
}
}

public static SectionSize valueOf(int sectionSize) {
return map.get(sectionSize);
}

public int getValue() {
return value;
}
}
6 changes: 6 additions & 0 deletions omegarecyclerview/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@
<enum name="header" value="0" />
<enum name="footer" value="1" />
</attr>
<attr name="sectionType" format="enum">
<enum name="Custom" value="-1" />
<enum name="Full" value="0" />
<enum name="Default" value="1" />
</attr>
<attr name="spanSize" format="integer"/>
<attr name="layout_showDivider" format="boolean" />
</declare-styleable>

Expand Down

0 comments on commit 980b637

Please sign in to comment.