Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Section gap is gone when expand the last item of section #516

Closed
steventxc opened this issue Jan 10, 2018 · 11 comments
Closed

Section gap is gone when expand the last item of section #516

steventxc opened this issue Jan 10, 2018 · 11 comments
Milestone

Comments

@steventxc
Copy link

as above describe, when the section contains a expandable item at last, expand it, the gap is gone, but collapse it, the gap is visible as normal. it's weird. any suggestion for it ? thx.

@steventxc
Copy link
Author

collapsed:
104245

expanded:
104310

@davideas
Copy link
Owner

davideas commented Jan 11, 2018

Could you call invalidateItemDecorations(delay) when expanding?
The RV should apply the decoration to the current view during a layout pass, but is calculated and applied if the next item is a header. So, if we insert a new view type the decoration is lost.

@steventxc
Copy link
Author

@davideas I'm not sure where to insert invalidateItemDecorations(delay) method.
i try insert code these place:

  1. in ExpandableViewHolder:

    @Override
    protected boolean shouldNotifyParentOnClick() {
        return true;
    }
    
    @Override
    protected void expandView(int position) {
        mAdapter.invalidateItemDecorations(100);
        super.expandView(position);
    }
    
  2. in AbstractExpandableItem :

     @Override
     public void bindViewHolder(FlexibleAdapter adapter, ES_ViewHolder holder, int position, List<Object> payloads) {
     	holder.tv_userName.setText(userName);
     	holder.tv_suggestion.setText(suggestion);
     	holder.iv_status.setImageResource(iconRes);
     
     	adapter.invalidateItemDecorations(100);
     }
    
  3. in Fragment when init adapter:

    List<ISectionable> list = new ArrayList<>();
    
    // section 1
    FDHeader header1 = new FDHeader(1, "Header 1");
    FDEatableSection section1 = new FDEatableSection(header1, "section1_1",
                        null, R.drawable.ic_eat_half);
    section1.addSubItem(new FDEatableSubItem(0, "section sub 1_1_1"));
    list.add(section1);
    
    // section 2
    FDHeader header2 = new FDHeader(2, "Header 2");
    
    FDEatableSection section2_1 = new FDEatableSection(header2, "section2_1", 
                        null, R.drawable.ic_eat_half);
    section2_1.addSubItem(new FDEatableSubItem(1, "section sub 2_1_1"));
    section2_1.addSubItem(new FDEatableSubItem(2, "section sub 2_1_2"));
    list.add(section2_1);
    
    // ....
    
    mAdapter = new FlexibleAdapter<ISectionable>(list,
                        FoodDetailFragment1.this, true);
    
    mAdapter.setDisplayHeadersAtStartUp(true)
        .setMinCollapsibleLevel(1) 
        .setAutoCollapseOnExpand(true)
        .expandAll(0); 
    
    
    recyclerView.setAdapter(mAdapter);
    recyclerView.addItemDecoration(new FlexibleItemDecoration(_mActivity)
                        .withSectionGapOffset(24));
    
    // i'm not sure this is necessary             
    mAdapter.invalidateItemDecorations(100);
                
    

but all above are not work. :(

especially the third point, some items will expand at the beginning, how could i handle the gap?

@davideas
Copy link
Owner

@steventxc, try 1st point after the super() call since the expand occurs inside there.

@steventxc
Copy link
Author

@davideas i followed your instruction, but it was not work.
so i put my code in this address, am i do something wrong?

@davideas
Copy link
Owner

@steventxc, the FDSubItem is not a ISectionable type, when I created the gap, it was only for 1 level of section with items of type ISectionable.
But now I need more time, I'm too busy in these days.

@steventxc
Copy link
Author

@davideas Thanks your time and help.
It's work when I implement the ISectionable interface.

@davideas
Copy link
Owner

davideas commented Jan 17, 2018

@steventxc, but which header do you assign? I tried and when no header is assigned it works nicely, but it will have an impact for your sticky header if activated, it won't work as expected.
I will have to find another way to create the gap. please keep this issue open.

@davideas
Copy link
Owner

davideas commented Jan 17, 2018

This will be the condition in FlexibleItemDecoration to apply section gap from the next version:

// IMPORTANT: the check must be done on the BOTTOM of the section,
// otherwise the sticky header will jump!
if (flexibleAdapter.isHeader(nextItem)) { // = nextItem instanceof IHeader
    Log.v("applySectionGap position=%s", position);
    if (orientation == RecyclerView.VERTICAL) {
        outRect.bottom += mSectionOffset;
    } else {
        outRect.right += mSectionOffset;
    }
}
if (position >= adapter.getItemCount() - mSectionGapOnLastItem) {
    Log.v("applySectionGapOnLastPosition position=%s", position);
    if (orientation == RecyclerView.VERTICAL) {
        outRect.bottom += mSectionOffset;
    } else {
        outRect.right += mSectionOffset;
    }
}

But for IExpandable, we will have always to invalidate the item decoration when expanding and collapsing!

So we don't need ISectionable anymore for subItems in expandables.

@davideas
Copy link
Owner

I was smarter and I was able to identify the header of the section anyway. That's why sticky header works if you don't apply any header. However, i think is not good to implement ISectionable just to have the gap, the interface covers another purpose.

@steventxc
Copy link
Author

@davideas as you said above, in my project, i never assign any header for subitem, just implement ISectionable, it works fine :)
thanks for your excellent and also SMART job! 😁

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

No branches or pull requests

2 participants