Search code examples
androidsuperslim

Android SuperSLiM: Margin between sections


I'm using the SuperSLiM library to recreate the agenda view from the google calendar app. The problem I'm having is to create a margin between sections. I tried putting a topMargin when it's the sections first position, but then the headers wouldn't appear correct.


Solution

  • Solved it. I give a boolean to my LineItem to say it is last and then apply a bottomMargin to it.

    LineItem:

    private class LineItem
    {
        public int sectionFirstPosition;
        public boolean isHeader;
        public boolean isLast;
        public Item item;
    
        public LineItem(Item item, boolean isHeader, int sectionFirstPosition)
        {
            this.isHeader = isHeader;
            this.item = item;
            this.sectionFirstPosition = sectionFirstPosition;
        }
    
        public void setLast()
        {
            isLast = true;
        }
    }
    

    Settings last item:

        for (int i = 0; i < items.size(); i++)
        {
            long header = item.get(i).getTimestamp();
            if (lastHeader != header)
            {
                if(mItems.size() >= 1)
                {
                    mItems.get(mItems.size() - 1).setLast();
                }
                // Insert new header view and update section data.
                sectionFirstPosition = i + headerCount;
                lastHeader = header;
                headerCount += 1;
                mItems.add(new LineItem(items.get(i), true, sectionFirstPosition));
            }
            mItems.add(new LineItem(items.get(i), false, sectionFirstPosition));
        }
    

    Set bottom margin:

        if(item.isLast)
        {
            lp.bottomMargin = mContext.getResources().getDimensionPixelSize(R.dimen.section_margin);
        }else
        {
            lp.bottomMargin = 0;
        }