Skip to content

Commit 2880ac5

Browse files
author
Rafael Dominiquini
committed
Fix lib for usage in GridLayoutManager
1 parent e7a9df7 commit 2880ac5

File tree

5 files changed

+32
-6
lines changed

5 files changed

+32
-6
lines changed

build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,6 @@ ext {
2424
supportLibsVersion = '23.3.0'
2525
targetSdkVersion = 23
2626
minSdkVersion = 11
27-
versionCode = 15
28-
versionName = "0.5.5"
27+
versionCode = 16
28+
versionName = "0.5.6"
2929
}

library/src/main/java/com/timehop/stickyheadersrecyclerview/HeaderPositionCalculator.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public boolean hasNewHeader(int position, boolean isReverseLayout) {
7575
return false;
7676
}
7777

78-
int numColumns = mAdapter.getNumColumns();
78+
int numColumns = mAdapter.getNumColumns() - mAdapter.getSpanSize(position) + 1;
7979
int columnOfItem = position % numColumns;
8080
if (columnOfItem > 0) {
8181
int firstItemOnRowPosition = position - columnOfItem;
@@ -94,6 +94,7 @@ public boolean hasNewHeader(int position, boolean isReverseLayout) {
9494
if (!indexOutOfBounds(nextItemPosition)){
9595
nextItemHeaderId = mAdapter.getHeaderId(nextItemPosition);
9696
}
97+
9798
int firstItemPosition = isReverseLayout? mAdapter.getItemCount()-1 : 0;
9899

99100
return position == firstItemPosition || headerId != nextItemHeaderId;

library/src/main/java/com/timehop/stickyheadersrecyclerview/StickyRecyclerHeadersAdapter.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,12 @@ public interface StickyRecyclerHeadersAdapter<VH extends RecyclerView.ViewHolder
4242
* @return the number of columns
4343
*/
4444
int getNumColumns();
45+
46+
/**
47+
* Returns the number of span occupied by the item at <code>position</code>.
48+
*
49+
* @param position The adapter position of the item
50+
* @return The number of spans occupied by the item at the provided position
51+
*/
52+
int getSpanSize(int position);
4553
}

library/src/main/java/com/timehop/stickyheadersrecyclerview/decorators/StickyRecyclerHeadersDecoration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public void onDrawOver(Canvas canvas, RecyclerView parent, RecyclerView.State st
109109

110110
int position = parent.getChildAdapterPosition(itemView);
111111

112-
if (position == RecyclerView.NO_POSITION || position % mAdapter.getNumColumns() > 0) {
112+
if (position == RecyclerView.NO_POSITION || position % (mAdapter.getNumColumns() - mAdapter.getSpanSize(position) + 1) > 0) {
113113
continue;
114114
}
115115

sample/src/main/java/com/timehop/stickyheadersrecyclerview/sample/MainActivity.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
import android.os.Handler;
99
import android.os.Looper;
1010
import android.support.v7.app.AppCompatActivity;
11+
import android.support.v7.widget.GridLayoutManager;
1112
import android.support.v7.widget.LinearLayoutManager;
1213
import android.support.v7.widget.RecyclerView;
13-
import android.support.v7.widget.StaggeredGridLayoutManager;
1414
import android.text.TextUtils;
1515
import android.util.Log;
1616
import android.view.LayoutInflater;
@@ -68,10 +68,17 @@ public void run() {
6868

6969
// Set layout manager
7070
int orientation = getLayoutManagerOrientation(getResources().getConfiguration().orientation);
71-
final StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(NUM_COLUMNS, orientation);
71+
final GridLayoutManager layoutManager = new GridLayoutManager(this, NUM_COLUMNS, orientation, false);
7272
layoutManager.setReverseLayout(isReverseButton.isChecked());
7373
recyclerView.setLayoutManager(layoutManager);
7474

75+
layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
76+
@Override
77+
public int getSpanSize(int position) {
78+
return adapter.getSpanSize(position);
79+
}
80+
});
81+
7582
// Add the sticky headers decoration
7683
final StickyRecyclerHeadersDecoration headersDecor = new StickyRecyclerHeadersDecoration(adapter, true);
7784
recyclerView.addItemDecoration(headersDecor);
@@ -169,6 +176,7 @@ public long getHeaderId(int position) {
169176
@Override
170177
public RecyclerView.ViewHolder onCreateHeaderViewHolder(ViewGroup parent, int position) {
171178
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.view_header, parent, false);
179+
172180
return new RecyclerView.ViewHolder(view) {
173181
};
174182
}
@@ -186,13 +194,22 @@ public int getNumColumns() {
186194
return NUM_COLUMNS;
187195
}
188196

197+
@Override
198+
public int getSpanSize(int position) {
199+
return isVowel((char) getHeaderId(position)) ? NUM_COLUMNS : 1;
200+
}
201+
189202
@Override
190203
public void updateAdapter() {
191204
reorderItems();
192205

193206
super.updateAdapter();
194207
}
195208

209+
private boolean isVowel(char c) {
210+
return "AEIOUaeiou".indexOf(c) != -1;
211+
}
212+
196213
private int getRandomColor() {
197214
SecureRandom rgen = new SecureRandom();
198215
return Color.HSVToColor(150, new float[]{

0 commit comments

Comments
 (0)