Skip to content

Commit

Permalink
Merge pull request #132 from Omega-R/develop
Browse files Browse the repository at this point in the history
Middle sticky, fixed divider
  • Loading branch information
anton-knyazev authored May 20, 2019
2 parents 2f84693 + 2a7fc4d commit e6701e0
Show file tree
Hide file tree
Showing 19 changed files with 533 additions and 356 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@

import com.omega_r.libs.omegarecyclerview.OmegaRecyclerView;
import com.omega_r.libs.omegarecyclerview.expandable_recycler_view.OmegaExpandableRecyclerView;
import com.omega_r.libs.omegarecyclerview.sticky_header.StickyHeaderAdapter;
import com.omega_r.libs.omegarecyclerview.sticky_header.BaseStickyHeaderDecoration;
import com.omega_r.libs.omegarecyclerview.expandable_recycler_view.data.ExpandableViewData;
import com.omega_r.libs.omegarecyclerview.sticky_decoration.StickyAdapter;
import com.omega_r.libs.omegarecyclerview.sticky_decoration.HeaderStickyDecoration;
import com.omega_r.omegarecyclerview.R;
import com.omega_r.omegarecyclerview.expandable_example.core.QuoteGlobalInfo;

public class DefaultStickyAdapter extends OmegaExpandableRecyclerView.Adapter<QuoteGlobalInfo, String> implements StickyHeaderAdapter<DefaultStickyAdapter.StickyViewHolder> {
public class DefaultStickyAdapter extends OmegaExpandableRecyclerView.Adapter<QuoteGlobalInfo, String> implements StickyAdapter<DefaultStickyAdapter.StickyViewHolder> {

@Override
protected ExGroupViewHolder provideGroupViewHolder(@NonNull ViewGroup viewGroup) {
Expand All @@ -25,18 +26,20 @@ protected ExChildViewHolder provideChildViewHolder(@NonNull ViewGroup viewGroup)
}

@Override
public long getHeaderId(int position) {
Integer providedId = getItem(position).getStickyId();
return providedId == null ? BaseStickyHeaderDecoration.NO_HEADER_ID : providedId;
public long getStickyId(int position) {
ExpandableViewData<QuoteGlobalInfo, String> item = getItem(position);
if (item == null) return HeaderStickyDecoration.NO_STICKY_ID;
Integer providedId = item.getStickyId();
return providedId == null ? HeaderStickyDecoration.NO_STICKY_ID : providedId;
}

@Override
public StickyViewHolder onCreateHeaderViewHolder(ViewGroup parent) {
public StickyViewHolder onCreateStickyViewHolder(ViewGroup parent) {
return new StickyViewHolder(parent);
}

@Override
public void onBindHeaderViewHolder(StickyViewHolder viewHolder, int position) {
public void onBindStickyViewHolder(StickyViewHolder viewHolder, int position) {
viewHolder.bind(getItem(position).getGroup().getYear());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
import android.widget.TextView;

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

public class StickyHeaderAdapter extends OmegaRecyclerView.Adapter<StickyHeaderAdapter.ViewHolder>
implements com.omega_r.libs.omegarecyclerview.sticky_header.StickyHeaderAdapter<StickyHeaderAdapter.HeaderHolder> {
implements StickyAdapter<StickyHeaderAdapter.HeaderHolder> {

private LayoutInflater mInflater;

Expand All @@ -35,19 +36,19 @@ public int getItemCount() {
}

@Override
public long getHeaderId(int position) {
public long getStickyId(int position) {
return (long) position / 6;
}

@Override
public HeaderHolder onCreateHeaderViewHolder(ViewGroup parent) {
public HeaderHolder onCreateStickyViewHolder(ViewGroup parent) {
View view = mInflater.inflate(R.layout.sticky_header_test, parent, false);
return new HeaderHolder(view);
}

@Override
public void onBindHeaderViewHolder(HeaderHolder viewHolder, int position) {
viewHolder.header.setText("Header " + getHeaderId(position));
public void onBindStickyViewHolder(HeaderHolder viewHolder, int position) {
viewHolder.header.setText("Header " + getStickyId(position));
}

static class ViewHolder extends OmegaRecyclerView.ViewHolder {
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/sticky_header_test.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_width="150dp"
android:layout_height="50dp"
android:background="@android:color/black"
android:gravity="center_vertical"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,40 +7,41 @@
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import androidx.annotation.ColorInt;
import androidx.annotation.ColorRes;
import androidx.annotation.IdRes;
import androidx.annotation.LayoutRes;
import androidx.annotation.Nullable;
import androidx.annotation.StringRes;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.ExpandedRecyclerView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

import com.omega_r.libs.omegarecyclerview.expandable_recycler_view.ExpandableStickyHeaderDecoration;
import com.omega_r.libs.omegarecyclerview.expandable_recycler_view.OmegaExpandableRecyclerView;
import com.omega_r.libs.omegarecyclerview.header.HeaderFooterWrapperAdapter;
import com.omega_r.libs.omegarecyclerview.item_decoration.DividerItemDecoration;
import com.omega_r.libs.omegarecyclerview.item_decoration.SpaceItemDecoration;
import com.omega_r.libs.omegarecyclerview.pagination.OnPageRequestListener;
import com.omega_r.libs.omegarecyclerview.pagination.PageRequester;
import com.omega_r.libs.omegarecyclerview.pagination.PaginationAdapter;
import com.omega_r.libs.omegarecyclerview.pagination.WrapperAdapter;
import com.omega_r.libs.omegarecyclerview.sticky_header.BaseStickyHeaderDecoration;
import com.omega_r.libs.omegarecyclerview.sticky_header.StickyHeaderAdapter;
import com.omega_r.libs.omegarecyclerview.sticky_header.StickyHeaderDecoration;
import com.omega_r.libs.omegarecyclerview.sticky_decoration.BaseStickyDecoration;
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.MiddleStickyDecoration;
import com.omega_r.libs.omegarecyclerview.swipe_menu.SwipeMenuHelper;

import java.util.ArrayList;
import java.util.List;
import java.util.WeakHashMap;

import androidx.annotation.ColorInt;
import androidx.annotation.ColorRes;
import androidx.annotation.IdRes;
import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.StringRes;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.ExpandedRecyclerView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

public class OmegaRecyclerView extends ExpandedRecyclerView implements SwipeMenuHelper.Callback {

private static final int[] DEFAULT_DIVIDER_ATTRS = new int[]{android.R.attr.listDivider};
Expand All @@ -50,7 +51,8 @@ public class OmegaRecyclerView extends ExpandedRecyclerView implements SwipeMenu

private SwipeMenuHelper mSwipeMenuHelper;
private PageRequester mPageRequester = new PageRequester();
private StickyHeaderDecoration mStickyHeaderDecoration;
private BaseStickyDecoration mBaseStickyDecoration;
private int mStickyMode = StickyAdapter.Mode.HEADER;
@LayoutRes
private int mPaginationLayout = R.layout.pagination_omega_layout;
@LayoutRes
Expand All @@ -62,6 +64,7 @@ public class OmegaRecyclerView extends ExpandedRecyclerView implements SwipeMenu
@Nullable
private DividerItemDecoration mDividerItemDecoration;
private int mItemSpace;
private int mDividerSize;

public OmegaRecyclerView(Context context) {
super(context);
Expand All @@ -87,6 +90,7 @@ private void init(Context context, @Nullable AttributeSet attrs, int defStyleAtt
initDivider(a);
initEmptyView(a);
initPagination(a);
initStickyMode(a);
a.recycle();
}
mSwipeMenuHelper = new SwipeMenuHelper(getContext(), this);
Expand Down Expand Up @@ -125,13 +129,15 @@ public void initDivider(TypedArray a) {
}
}

int dividerHeight = (int) a.getDimension(R.styleable.OmegaRecyclerView_dividerHeight,

mDividerSize = (int) a.getDimension(R.styleable.OmegaRecyclerView_dividerHeight,

a.getDimension(R.styleable.OmegaRecyclerView_android_dividerHeight, -1));
float alpha = a.getFloat(R.styleable.OmegaRecyclerView_dividerAlpha, 1);

mDividerItemDecoration = new DividerItemDecoration(
dividerDrawable,
dividerHeight,
mDividerSize,
showDivider,
mItemSpace / 2,
alpha
Expand Down Expand Up @@ -162,14 +168,18 @@ private void initPagination(TypedArray a) {
}
}

private void initStickyMode(TypedArray a) {
mStickyMode = a.getInt(R.styleable.OmegaRecyclerView_stickyMode, mStickyMode);
}

@Override
@SuppressWarnings("unchecked")
public void setAdapter(RecyclerView.Adapter adapter) {
unregisterObservers();

if (adapter == null) {
super.setAdapter(null);
updateStickyHeader(null);
updateStickyDecoration(null);
return;
}

Expand All @@ -192,7 +202,7 @@ public void setAdapter(RecyclerView.Adapter adapter) {
mPageRequester.reset();

registerObservers(shellAdapter);
updateStickyHeader(shellAdapter);
updateStickyDecoration(shellAdapter);
}

@Nullable
Expand Down Expand Up @@ -228,50 +238,50 @@ private void registerObservers(RecyclerView.Adapter adapter) {
adapter.registerAdapterDataObserver(mEmptyObserver);
}

private void updateStickyHeader(@Nullable RecyclerView.Adapter adapter) {
protected void updateStickyDecoration(@Nullable RecyclerView.Adapter adapter) {
if (adapter == null) {
if (mStickyHeaderDecoration != null) removeItemDecoration(mStickyHeaderDecoration);
if (mBaseStickyDecoration != null) removeItemDecoration(mBaseStickyDecoration);
} else {
StickyHeaderAdapter stickyHeaderAdapter = null;
OmegaExpandableRecyclerView.Adapter expandableAdapter = null;

StickyAdapter stickyAdapter = null;
if (adapter instanceof WrapperAdapter) {
RecyclerView.Adapter wrappedAdapter = ((WrapperAdapter) adapter).getLastWrappedAdapter();
if (wrappedAdapter instanceof StickyHeaderAdapter) {
stickyHeaderAdapter = (StickyHeaderAdapter) wrappedAdapter;
if (wrappedAdapter instanceof StickyAdapter) {
stickyAdapter = (StickyAdapter) wrappedAdapter;
}
} else if (adapter instanceof HeaderFooterWrapperAdapter) {
if (((HeaderFooterWrapperAdapter) adapter).getStickyHeaderAdapter() != null) {
stickyHeaderAdapter = (StickyHeaderAdapter) adapter;
if (((HeaderFooterWrapperAdapter) adapter).getStickyAdapter() != null) {
stickyAdapter = (StickyAdapter) adapter;
}
} else if (adapter instanceof StickyHeaderAdapter) {
stickyHeaderAdapter = (StickyHeaderAdapter) adapter;
} else if (adapter instanceof StickyAdapter) {
stickyAdapter = (StickyAdapter) adapter;
}

if (adapter instanceof OmegaExpandableRecyclerView.Adapter) {
expandableAdapter = (OmegaExpandableRecyclerView.Adapter) adapter;
}

if (mStickyHeaderDecoration == null) {
mStickyHeaderDecoration = provideStickyHeaderDecoration(stickyHeaderAdapter, expandableAdapter);
if (mStickyHeaderDecoration == null) return;
mStickyHeaderDecoration.setItemSpace(mItemSpace);
addItemDecoration(mStickyHeaderDecoration);
if (mBaseStickyDecoration == null) {
mBaseStickyDecoration = provideStickyDecoration(adapter, stickyAdapter);
if (mBaseStickyDecoration == null) return;
mBaseStickyDecoration.setItemSpace(mItemSpace + mDividerSize);
addItemDecoration(mBaseStickyDecoration);
} else {
mStickyHeaderDecoration.setStickyHeaderAdapter(stickyHeaderAdapter);
if (mStickyHeaderDecoration instanceof ExpandableStickyHeaderDecoration) {
((ExpandableStickyHeaderDecoration) mStickyHeaderDecoration).setExpandableAdapter(expandableAdapter);
}
mBaseStickyDecoration.setStickyAdapter(stickyAdapter);
invalidateItemDecorations();
}
}
}

@Nullable
protected StickyHeaderDecoration provideStickyHeaderDecoration(@Nullable StickyHeaderAdapter adapter,
@Nullable OmegaExpandableRecyclerView.Adapter expandableAdapter) {
if (adapter == null) return null;
return new BaseStickyHeaderDecoration(adapter);
protected BaseStickyDecoration provideStickyDecoration(@NonNull RecyclerView.Adapter adapter, @Nullable StickyAdapter stickyAdapter) {
if (stickyAdapter == null) return null;
switch (mStickyMode) {
case StickyAdapter.Mode.MIDDLE:
return new MiddleStickyDecoration(stickyAdapter);
case StickyAdapter.Mode.HEADER:
default:
return new HeaderStickyDecoration(stickyAdapter);
}
}

@Nullable
protected final BaseStickyDecoration getStickyDecoration() {
return mBaseStickyDecoration;
}


Expand All @@ -282,10 +292,10 @@ public void addView(View view, int index, ViewGroup.LayoutParams params) {
} else {
view.setLayoutParams(params);
SectionState sectionState = mLayoutParamCache.get(params);
Integer integer = sectionState.position;
if (sectionState == null) return;
view.setTag(R.id.section_show_divider, sectionState.showDivider);

if (integer == 0) {
if (sectionState.position == 0) {
mHeadersList.add(view);
} else {
mFooterList.add(view);
Expand Down
Loading

0 comments on commit e6701e0

Please sign in to comment.