grennis / ExpandableRecyclerView

Recycler view adapter that adds expandable / collapsible items
MIT License
85 stars 40 forks source link

GridLayoutManager usage #7

Open IamEgor opened 8 years ago

IamEgor commented 8 years ago

Greetings!

I'm trying to use this lib with GridLayoutManager. But when I've done this, the result was not as I expected.

device-2016-02-25-015937 device-2016-02-25-020010 device-2016-02-25-020050

When all tabs are expanded thats fine. In other cases it's looked wired. I use this code in my Activity.

final GridLayoutManager manager = new GridLayoutManager(this, 2);
        recycler.setLayoutManager(manager);

        final PeopleAdapter adapter = new PeopleAdapter(this);
        recycler.setAdapter(adapter);

        manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                return adapter.isHeader(position) ? manager.getSpanCount() : 1;
            }
        });
grennis commented 8 years ago

Interesting, what does your implementation of isHeader look like?

IamEgor commented 8 years ago

I just save list.

private List<PeopleListItem> items;

    private List<PeopleListItem> items;

    public PeopleAdapter(Context context) {
        super(context);

        items = getSampleItems();
        setItems(items);
    }

Add method to PeopleAdapter.

public boolean isHeader(int location) {
        return items.get(location).isHeader();
    }

Save into variable type(Header or not) and add method to PeopleListItem.

public static class PeopleListItem extends ExpandableRecyclerAdapter.ListItem {

        public String Text;
        private int type;

        public PeopleListItem(String group) {
            super(TYPE_HEADER);

            type = TYPE_HEADER;
            Text = group;
        }

        public PeopleListItem(String first, String last) {
            super(TYPE_PERSON);

            type = TYPE_PERSON;
            Text = first + " " + last;
        }

        public boolean isHeader() {
            return type == TYPE_HEADER;
        }

    }
grennis commented 8 years ago

It must have something to do with the fact that as the groups are expanded/collapsed, the positions of the headers change. The grid layout doesn't realize that the column spans are changing at those positions. I will need to look into it. I've never tried to use it with grid layout manager.