question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to merge new items with same Header

See original GitHub issue

If I use FlexibleAdapter with sticky Headers, how can I add new List of items to the adapter which may contain Header that is already in the list?

Like:

List<ISectionable> originalItems = new ArrayList();

IHeader headerA1 = new Header(1);
ISectionable itemA1 = new Item(A1);
itemA1.setHeader(headerA1);
originalItems.add(itemA1);

ISectionable itemA2 = new Item(12);
itemA2.setHeader(headerA1);
originalItems.add(itemA2);
// ...originalItems contains more Headers and items as well

FlexibleAdapter adapter = new FlexibleAdapter(originalItems);
// .. setup

List<ISectionable> newItems = new ArrayList();

IHeader headerB1 = new Header(1);
ISectionable itemB1 = new Item(B1);
itemB1.setHeader(headerB1);
newItems.add(itemB1);
// ...newItems contains more headers and items


// How to add newItems to the list to have only one Header(1)?

// Even though headerA1.equals(headerB1) is true, addItems() will result in a second Header(1) added to the list:
adapter.addItems(0, newItems);

So does FlexibleAdapter support some automatic merging of the headers that are equal or do I have to do that manually by using the correct references of the actual header objects? Either by using addItemToSection(), or with addItems() but with replacing headerB1 with headerA1 for the items in newItems before adding them.

Thanks.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:16 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
lemonbostoncommented, Feb 9, 2017

We don’t really need a sorting and comparison because the items are provided in correct order by our API and I think the Comparator wouldn’t have helped, because as I come to understand the problem is simply that when adding items to a RecycerView to a postion which is visible, it of course shows them and pushes everything down. And because the IHeader was a visible item, adding anything under it naturally pushes the rest down. Only if items are added above it (at position 0), then RecyclerView adds them ‘outside’ of the screen. During trying out different ways to solve this, I finally ‘stumbled upon’ one that worked: simply removing the IHeader item before adding the new items.

This is how I do it now and seems to work:

    private void mergeNewItemsTop(List<IFlexible> newItems)
    {
        IHeader firstDayInCurrent = getFirstHeaderCurrent();
        IHeader lastDayInNew = getLastHeader(newItems);

        if (Objects.equals(lastDayInNew, firstDayInCurrent))
        {
            mFlexibleAdapter.removeItem(mFlexibleAdapter.getGlobalPositionOf(firstDayInCurrent));
            int lastItemWithDistinctHeader = -1;
            for (int i = newItems.size() - 1; i >= 0; i--)
            {
                if (newItems.get(i) instanceof ISectionable)
                {
                    ISectionable item = ((ISectionable) newItems.get(i));
                    if (item.getHeader().equals(lastDayInNew))
                    {
                        item.setHeader(firstDayInCurrent);
                        mFlexibleAdapter.addItemToSection(item, firstDayInCurrent, 0);
                    }
                    else
                    {
                        lastItemWithDistinctHeader = i;
                        break;
                    }
                }
            }
            List<IFlexible> distinctItems = newItems.subList(0, ++lastItemWithDistinctHeader);
            mFlexibleAdapter.addItems(0, distinctItems);
        }
        else
        {
            mFlexibleAdapter.addItems(0, newItems);
        }
    }
0reactions
lemonbostoncommented, Jul 6, 2017

I am afraid we don’t have time for checking it now thoroughly so I wouldn’t like to touch that code currently unless it’s necessary, to avoid getting bogged down or causing any side effects. But it’s good to hear that there were improvements and thanks for letting me know. I’ll keep in mind to update the version and check back on the workarounds when we need to touch that part of the code base again.

Feel free to close this issue of course, I will create a new one if I encounter anything later.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to merge multiple sheets with same headers in Excel?
1. Activate the workbook you want to merge the sheets, press Alt + F11 keys to open Microsoft Visual Basic for Applications window....
Read more >
Solved: Merging Tables with same Headers
For the first part, you would use "Append Queries" rather than mergeing them. It is all the way on the right side of...
Read more >
Combine Excel tables based on common headers - Ablebits
Combine Excel tables based on common headers · Start Combine Sheets · Step 1: Select worksheets and ranges to join · Step 2:...
Read more >
Merge columns with same header containing split data
1 Answer 1 · Navigate to Tools > References · Scroll down and select Microsoft Scripting Runtime · Click anywhere inside the code...
Read more >
How can I merge two or more tables? - Microsoft Support
Merge two tables using the VLOOKUP function · Copy the headings Sales ID and Region in the Orange table (only those two cells)....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found