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

Item order not respected when using SpanSize width = number of columns #39

Open
DennyWeinberg opened this issue Nov 30, 2018 · 3 comments

Comments

@DennyWeinberg
Copy link

I have the following code to collect my items to show in my RV:

    // Spanned
    public RecyclerView.LayoutManager getLayoutManager() {
        if (!mMainView || mDataHolder.getViewMode(mActivity).equals("grid")) { // We force the grid view in subset
            final Integer numColumns = UserPreferencesHandler.<Integer>readValue(mActivity, UserPreferencesHandler.PREF_KEY_NUM_COLUMNS);

            SpannedGridLayoutManager layoutManager = new SpannedGridLayoutManager(SpannedGridLayoutManager.Orientation.VERTICAL, numColumns);
            layoutManager.setSpanSizeLookup(new SpannedGridLayoutManager.SpanSizeLookup(position -> {
                try {
                    switch (RVAdapterTimeline.this.getItemViewType(position)) {
                        case RVAdapter.VIEW_TYPE_HEADER:
                            try {
                                return new SpanSize(numColumns, 1);
                            } catch (ClassCastException e) {
                                return new SpanSize(1, 1);
                            }
                        default:
                            LineItem lineitem = mItems.get(position);
                            if (lineitem != null && lineitem.mediaItem != null && lineitem.mediaItem.getRating() != null && lineitem.mediaItem.getRating() == 5) {
                                return new SpanSize(2, 2);
                            }

                            return new SpanSize(1, 1);
                    }
                } catch (IndexOutOfBoundsException e) {
                    MyApplication.toastSomething(mActivity, e); // Seen on firebase
                    return new SpanSize(1, 1);
                }
            }));

            return layoutManager;
        } else {
            return new LinearLayoutManager(mActivity, LinearLayoutManager.VERTICAL, false);
        }
    }

There are header items with a full column span:
return new SpanSize(numColumns, 1);
Other items are with span width 2 or 2. The number of columns is 5.

After collecting the items, they are used for the RV adapter. The problem is that the order in the rendered RV is not the same as the items. Result:
Pic

Could you please fix that? I can confirm 100% that the default GridLayoutManager is working with these items.

@jmartinesp
Copy link
Owner

I'm afraid that's not possible at the moment. The LayoutManager will order items so they don't leave any gaps. That means that if an item with can fit in a previous position, it will be placed there.

That said, the LayoutManager could be modified to support sections and only do this kind of ordering inside those sections. However, tomorrow I'm leaving for a business trip and I won't be back for at least a week, so I don't think I'll have time to look at this until then.

@DennyWeinberg
Copy link
Author

Ok. Thanks for your message. Maybe you could check if the span is equal to the number of columns, in this case, you don't fill the gaps. This would be an easy workaround. I understand the problem.

@DennyWeinberg
Copy link
Author

Still waiting for this fix... Is there another lib I could use?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants