Skip to content
This repository has been archived by the owner on Mar 24, 2023. It is now read-only.

Support GridLayoutManager #106

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,13 @@ public boolean hasNewHeader(int position, boolean isReverseLayout) {
return false;
}

int numColumns = mAdapter.getNumColumns();
int columnOfItem = position % numColumns;
if (columnOfItem > 0) {
int firstItemOnRowPosition = position - columnOfItem;
return hasNewHeader(firstItemOnRowPosition, isReverseLayout);
}

long headerId = mAdapter.getHeaderId(position);

if (headerId < 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,10 @@ public interface StickyRecyclerHeadersAdapter<VH extends RecyclerView.ViewHolder
* @return the number of views in the adapter
*/
int getItemCount();


/**
* @return the number of columns
*/
int getNumColumns();
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ public void onDrawOver(Canvas canvas, RecyclerView parent, RecyclerView.State st
for (int i = 0; i < childCount; i++) {
View itemView = parent.getChildAt(i);
int position = parent.getChildAdapterPosition(itemView);
if (position == RecyclerView.NO_POSITION) {
if (position == RecyclerView.NO_POSITION ||
position % mAdapter.getNumColumns() > 0) {
continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import android.os.Handler;
import android.os.Looper;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
Expand All @@ -24,6 +25,8 @@

public class MainActivity extends AppCompatActivity {

private static final int NUM_COLUMNS = 3;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand All @@ -35,7 +38,8 @@ protected void onCreate(Bundle savedInstanceState) {

// Set adapter populated with example dummy data
final AnimalsHeadersAdapter adapter = new AnimalsHeadersAdapter();
adapter.add("Animals below!");
adapter.setNumColumns(NUM_COLUMNS);
// adapter.add("Animals below!");
adapter.addAll(getDummyDataSet());
recyclerView.setAdapter(adapter);

Expand All @@ -58,7 +62,9 @@ public void run() {

// Set layout manager
int orientation = getLayoutManagerOrientation(getResources().getConfiguration().orientation);
final LinearLayoutManager layoutManager = new LinearLayoutManager(this, orientation, isReverseButton.isChecked());
// final LinearLayoutManager layoutManager = new LinearLayoutManager(this, orientation, isReverseButton.isChecked());

final GridLayoutManager layoutManager = new GridLayoutManager(this, NUM_COLUMNS);
recyclerView.setLayoutManager(layoutManager);

// Add the sticky headers decoration
Expand Down Expand Up @@ -118,6 +124,8 @@ private int getLayoutManagerOrientation(int activityOrientation) {

private class AnimalsHeadersAdapter extends AnimalsAdapter<RecyclerView.ViewHolder>
implements StickyRecyclerHeadersAdapter<RecyclerView.ViewHolder> {
private int numColumns = 1;

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
Expand Down Expand Up @@ -163,5 +171,13 @@ private int getRandomColor() {
});
}

@Override
public int getNumColumns() {
return numColumns;
}

public void setNumColumns(int numColumns) {
this.numColumns = numColumns;
}
}
}
25 changes: 0 additions & 25 deletions sample/src/main/res/values/arrays.xml
Original file line number Diff line number Diff line change
Expand Up @@ -205,12 +205,9 @@
<item>Egyptian Mau</item>
<item>Electric Eel</item>
<item>Elephant</item>
<item>Elephant Seal</item>
<item>Elephant Shrew</item>
<item>Emperor Penguin</item>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was there a need to remove sample data to make this work?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah because each header needs to have a multiple of 3 items?

<item>Emperor Tamarin</item>
<item>Emu</item>
<item>English Cocker Spaniel</item>
<item>English Shepherd</item>
<item>English Springer Spaniel</item>
<item>Entlebucher Mountain Dog</item>
Expand All @@ -223,7 +220,6 @@
<item>Field Spaniel</item>
<item>Fin Whale</item>
<item>Finnish Spitz</item>
<item>Fire-Bellied Toad</item>
<item>Fish</item>
<item>Fishing Cat</item>
<item>Flamingo</item>
Expand All @@ -246,8 +242,6 @@
<item>Gentoo Penguin</item>
<item>Geoffroys Tamarin</item>
<item>Gerbil</item>
<item>German Pinscher</item>
<item>German Shepherd</item>
<item>Gharial</item>
<item>Giant African Land Snail</item>
<item>Giant Clam</item>
Expand Down Expand Up @@ -293,8 +287,6 @@
<item>Himalayan</item>
<item>Hippopotamus</item>
<item>Honey Bee</item>
<item>Horn Shark</item>
<item>Horned Frog</item>
<item>Horse</item>
<item>Horseshoe Crab</item>
<item>Howler Monkey</item>
Expand All @@ -310,7 +302,6 @@
<item>Indian Elephant</item>
<item>Indian Palm Squirrel</item>
<item>Indian Rhinoceros</item>
<item>Indian Star Tortoise</item>
<item>Indochinese Tiger</item>
<item>Indri</item>
<item>Insect</item>
Expand All @@ -320,16 +311,12 @@
<item>Jackal</item>
<item>Jaguar</item>
<item>Japanese Chin</item>
<item>Japanese Macaque</item>
<item>Javan Rhinoceros</item>
<item>Javanese</item>
<item>Jellyfish</item>
<item>Kakapo</item>
<item>Kangaroo</item>
<item>Keel Billed Toucan</item>
<item>Killer Whale</item>
<item>King Crab</item>
<item>King Penguin</item>
<item>Kingfisher</item>
<item>Kiwi</item>
<item>Koala</item>
Expand All @@ -345,7 +332,6 @@
<item>Leopard Cat</item>
<item>Leopard Seal</item>
<item>Leopard Tortoise</item>
<item>Liger</item>
<item>Lion</item>
<item>Lionfish</item>
<item>Little Penguin</item>
Expand Down Expand Up @@ -381,8 +367,6 @@
<item>Monitor Lizard</item>
<item>Monkey</item>
<item>Monte Iberia Eleuth</item>
<item>Moorhen</item>
<item>Moose</item>
<item>Moray Eel</item>
<item>Moth</item>
<item>Mountain Gorilla</item>
Expand All @@ -406,7 +390,6 @@
<item>Opossum</item>
<item>Orang-utan</item>
<item>Ostrich</item>
<item>Otter</item>
<item>Oyster</item>
<item>Pademelon</item>
<item>Panther</item>
Expand All @@ -416,8 +399,6 @@
<item>Pekingese</item>
<item>Pelican</item>
<item>Penguin</item>
<item>Persian</item>
<item>Pheasant</item>
<item>Pied Tamarin</item>
<item>Pig</item>
<item>Pika</item>
Expand Down Expand Up @@ -445,7 +426,6 @@
<item>Pygmy Marmoset</item>
<item>Quail</item>
<item>Quetzal</item>
<item>Quokka</item>
<item>Quoll</item>
<item>Rabbit</item>
<item>Raccoon</item>
Expand All @@ -455,7 +435,6 @@
<item>Rat</item>
<item>Rattlesnake</item>
<item>Red Knee Tarantula</item>
<item>Red Panda</item>
<item>Red Wolf</item>
<item>Red-handed Tamarin</item>
<item>Reindeer</item>
Expand All @@ -476,7 +455,6 @@
<item>Saola</item>
<item>Scorpion</item>
<item>Scorpion Fish</item>
<item>Sea Dragon</item>
<item>Sea Lion</item>
<item>Sea Otter</item>
<item>Sea Slug</item>
Expand Down Expand Up @@ -542,8 +520,6 @@
<item>Tiffany</item>
<item>Tiger</item>
<item>Tiger Salamander</item>
<item>Tiger Shark</item>
<item>Tortoise</item>
<item>Toucan</item>
<item>Tree Frog</item>
<item>Tropicbird</item>
Expand Down Expand Up @@ -583,7 +559,6 @@
<item>Woolly Mammoth</item>
<item>Woolly Monkey</item>
<item>Wrasse</item>
<item>X-Ray Tetra</item>
<item>Yak</item>
<item>Yellow-Eyed Penguin</item>
<item>Yorkshire Terrier</item>
Expand Down