Search in sources :

Example 11 with ISectionable

use of eu.davidea.flexibleadapter.items.ISectionable in project FlexibleAdapter by davideas.

the class FragmentHeadersSections method onParameterSelected.

@Override
public void onParameterSelected(int itemType, int referencePosition, int childPosition) {
    if (referencePosition < 0)
        return;
    int scrollTo, id;
    IHeader referenceHeader = getReferenceList().get(referencePosition);
    Log.d(TAG, "Adding New Item: ItemType=" + itemType + " referencePosition=" + referencePosition + " childPosition=" + childPosition);
    switch(itemType) {
        case //Expandable
        1:
            id = mAdapter.getItemCountOfTypes(R.layout.recycler_expandable_item) + 1;
            ISectionable sectionableExpandable = DatabaseService.newExpandableItem(id, referenceHeader);
            mAdapter.addItemToSection(sectionableExpandable, referenceHeader, childPosition);
            scrollTo = mAdapter.getGlobalPositionOf(referenceHeader);
            break;
        case //Expandable Header
        2:
            id = mAdapter.getItemCountOfTypes(R.layout.recycler_expandable_header_item) + 1;
            ExpandableHeaderItem expandableHeader = DatabaseService.newExpandableSectionItem(id);
            expandableHeader.setExpanded(false);
            mAdapter.addSection(expandableHeader, referenceHeader);
            scrollTo = mAdapter.getGlobalPositionOf(expandableHeader);
            break;
        case //Header
        3:
            id = mAdapter.getItemCountOfTypes(R.layout.recycler_header_item) + 1;
            IHeader header = DatabaseService.newHeader(id);
            mAdapter.addSection(header, referenceHeader);
            scrollTo = mAdapter.getGlobalPositionOf(header);
            break;
        default:
            //case 0 = Simple Item
            id = mAdapter.getItemCountOfTypes(R.layout.recycler_expandable_item) + 1;
            ISectionable sectionable = DatabaseService.newSimpleItem(id, referenceHeader);
            mAdapter.addItemToSection(sectionable, referenceHeader, childPosition);
            scrollTo = mAdapter.getGlobalPositionOf(referenceHeader);
    }
    // With Sticky Headers enabled, this seems necessary to give
    // time at the RV to be in correct state before scrolling
    final int scrollToFinal = scrollTo;
    mRecyclerView.post(new Runnable() {

        @Override
        public void run() {
            mRecyclerView.smoothScrollToPosition(scrollToFinal);
        }
    });
}
Also used : IHeader(eu.davidea.flexibleadapter.items.IHeader) ISectionable(eu.davidea.flexibleadapter.items.ISectionable) ExpandableHeaderItem(eu.davidea.samples.flexibleadapter.items.ExpandableHeaderItem)

Aggregations

ISectionable (eu.davidea.flexibleadapter.items.ISectionable)11 SuppressLint (android.annotation.SuppressLint)7 IHeader (eu.davidea.flexibleadapter.items.IHeader)6 ArrayList (java.util.ArrayList)2 NonNull (android.support.annotation.NonNull)1 FlexibleAdapter (eu.davidea.flexibleadapter.FlexibleAdapter)1 IExpandable (eu.davidea.flexibleadapter.items.IExpandable)1 ExpandableHeaderItem (eu.davidea.samples.flexibleadapter.items.ExpandableHeaderItem)1 List (java.util.List)1