Search in sources :

Example 6 with IHeader

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

the class HeadersSectionsTest method testGetHeaderOf.

@Test
public void testGetHeaderOf() throws Exception {
    mAdapter = new FlexibleAdapter<>(mItems);
    IHeader header = mAdapter.getHeaderOf(mItems.get(0));
    assertNotNull(header);
}
Also used : IHeader(eu.davidea.flexibleadapter.items.IHeader) Test(org.junit.Test)

Example 7 with IHeader

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

the class FlexibleAdapter method calculatePositionFor.

/**
	 * New method to extract the new position where the item should lay.
	 * <p><b>Note: </b>The {@code Comparator} object should be customized to support <u>all</u>
	 * types of items this Adapter is managing or a {@code ClassCastException} will be raised.</p>
	 * If the {@code Comparator} is {@code null} the returned position is 0 (first position).
	 *
	 * @param item       the item to evaluate the insertion
	 * @param comparator the Comparator object with the logic to sort the list
	 * @return the position resulted from sorting with the provided Comparator
	 * @since 5.0.0-b7
	 */
public int calculatePositionFor(@NonNull Object item, @Nullable Comparator comparator) {
    // There's nothing to compare
    if (comparator == null)
        return 0;
    // Header is visible
    if (item instanceof ISectionable) {
        IHeader header = ((ISectionable) item).getHeader();
        if (header != null && !header.isHidden()) {
            List sortedList = getSectionItems(header);
            sortedList.add(item);
            Collections.sort(sortedList, comparator);
            int itemPosition = mItems.indexOf(item);
            int headerPosition = getGlobalPositionOf(header);
            // #143 - calculatePositionFor() missing a +1 when addItem (fixed by condition: itemPosition != -1)
            // fix represents the situation when item is before the target position (used in moveItem)
            int fix = itemPosition != -1 && itemPosition < headerPosition ? 0 : 1;
            int result = headerPosition + sortedList.indexOf(item) + fix;
            if (DEBUG) {
                Log.v(TAG, "Calculated finalPosition=" + result + " sectionPosition=" + headerPosition + " relativePosition=" + sortedList.indexOf(item) + " fix=" + fix);
            }
            return result;
        }
    }
    // All other cases
    List sortedList = new ArrayList(mItems);
    if (!sortedList.contains(item))
        sortedList.add(item);
    Collections.sort(sortedList, comparator);
    if (DEBUG)
        Log.v(TAG, "Calculated position " + Math.max(0, sortedList.indexOf(item)) + " for item=" + item);
    return Math.max(0, sortedList.indexOf(item));
}
Also used : IHeader(eu.davidea.flexibleadapter.items.IHeader) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) ISectionable(eu.davidea.flexibleadapter.items.ISectionable) SuppressLint(android.annotation.SuppressLint)

Example 8 with IHeader

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

the class FlexibleAdapter method showHeaderOf.

/**
	 * Internal method to show/add a header in the internal list.
	 *
	 * @param position the position where the header will be displayed
	 * @param item     the item that holds the header
	 * @param init     for silent initialization: skip notifyItemInserted
	 * @since 5.0.0-b1
	 */
private boolean showHeaderOf(int position, @NonNull T item, boolean init) {
    // Take the header
    IHeader header = getHeaderOf(item);
    // Check header existence
    if (header == null || getPendingRemovedItem(item) != null)
        return false;
    if (header.isHidden()) {
        if (DEBUG)
            Log.v(TAG, "Showing header at position " + position + " header=" + header);
        header.setHidden(false);
        // Insert header, but skip notifyItemInserted when init=true!
        performInsert(position, Collections.singletonList((T) header), !init);
        return true;
    }
    return false;
}
Also used : IHeader(eu.davidea.flexibleadapter.items.IHeader)

Example 9 with IHeader

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

the class FlexibleAdapter method swapItems.

/**
	 * Swaps the elements of list at indices fromPosition and toPosition and notify the change.
	 * <p>Selection of swiped elements is automatically updated.</p>
	 *
	 * @param fromPosition previous position of the item.
	 * @param toPosition   new position of the item.
	 * @since 5.0.0-b7
	 */
public void swapItems(List<T> list, int fromPosition, int toPosition) {
    if (fromPosition < 0 || fromPosition >= getItemCount() || toPosition < 0 || toPosition >= getItemCount()) {
        return;
    }
    if (DEBUG) {
        Log.v(TAG, "swapItems from=" + fromPosition + " [selected? " + isSelected(fromPosition) + "] to=" + toPosition + " [selected? " + isSelected(toPosition) + "]");
    }
    // Collapse expandable before swapping (otherwise items are mixed badly)
    if (fromPosition < toPosition && isExpandable(getItem(fromPosition)) && isExpanded(toPosition)) {
        collapse(toPosition);
    }
    // Perform item swap (for all LayoutManagers)
    if (fromPosition < toPosition) {
        for (int i = fromPosition; i < toPosition; i++) {
            if (DEBUG)
                Log.v(TAG, "swapItems from=" + i + " to=" + (i + 1));
            Collections.swap(mItems, i, i + 1);
            swapSelection(i, i + 1);
        }
    } else {
        for (int i = fromPosition; i > toPosition; i--) {
            if (DEBUG)
                Log.v(TAG, "swapItems from=" + i + " to=" + (i - 1));
            Collections.swap(mItems, i, i - 1);
            swapSelection(i, i - 1);
        }
    }
    notifyItemMoved(fromPosition, toPosition);
    // Header swap linkage
    if (headersShown) {
        // Situation AFTER items have been swapped, items are inverted!
        T fromItem = getItem(toPosition);
        T toItem = getItem(fromPosition);
        int oldPosition, newPosition;
        if (toItem instanceof IHeader && fromItem instanceof IHeader) {
            if (fromPosition < toPosition) {
                // Dragging down fromHeader
                // Auto-linkage all section-items with new header
                IHeader header = (IHeader) fromItem;
                List<ISectionable> items = getSectionItems(header);
                for (ISectionable sectionable : items) {
                    linkHeaderTo((T) sectionable, header, Payload.LINK);
                }
            } else {
                // Dragging up fromHeader
                // Auto-linkage all section-items with new header
                IHeader header = (IHeader) toItem;
                List<ISectionable> items = getSectionItems(header);
                for (ISectionable sectionable : items) {
                    linkHeaderTo((T) sectionable, header, Payload.LINK);
                }
            }
        } else if (toItem instanceof IHeader) {
            // A Header is being swapped up
            // Else a Header is being swapped down
            oldPosition = fromPosition < toPosition ? toPosition + 1 : toPosition;
            newPosition = fromPosition < toPosition ? toPosition : fromPosition + 1;
            // Swap header linkage
            linkHeaderTo(getItem(oldPosition), getSectionHeader(oldPosition), Payload.LINK);
            linkHeaderTo(getItem(newPosition), (IHeader) toItem, Payload.LINK);
        } else if (fromItem instanceof IHeader) {
            // A Header is being dragged down
            // Else a Header is being dragged up
            oldPosition = fromPosition < toPosition ? fromPosition : fromPosition + 1;
            newPosition = fromPosition < toPosition ? toPosition + 1 : fromPosition;
            // Swap header linkage
            linkHeaderTo(getItem(oldPosition), getSectionHeader(oldPosition), Payload.LINK);
            linkHeaderTo(getItem(newPosition), (IHeader) fromItem, Payload.LINK);
        } else {
            // A Header receives the toItem
            // Else a Header receives the fromItem
            oldPosition = fromPosition < toPosition ? toPosition : fromPosition;
            newPosition = fromPosition < toPosition ? fromPosition : toPosition;
            // Swap header linkage
            T oldItem = getItem(oldPosition);
            IHeader header = getHeaderOf(oldItem);
            if (header != null) {
                IHeader oldHeader = getSectionHeader(oldPosition);
                if (oldHeader != null && !oldHeader.equals(header)) {
                    linkHeaderTo(oldItem, oldHeader, Payload.LINK);
                }
                linkHeaderTo(getItem(newPosition), header, Payload.LINK);
            }
        }
    }
}
Also used : IHeader(eu.davidea.flexibleadapter.items.IHeader) ISectionable(eu.davidea.flexibleadapter.items.ISectionable) SuppressLint(android.annotation.SuppressLint)

Example 10 with IHeader

use of eu.davidea.flexibleadapter.items.IHeader 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

IHeader (eu.davidea.flexibleadapter.items.IHeader)10 ISectionable (eu.davidea.flexibleadapter.items.ISectionable)6 SuppressLint (android.annotation.SuppressLint)5 Test (org.junit.Test)2 AbstractFlexibleItem (eu.davidea.flexibleadapter.items.AbstractFlexibleItem)1 IExpandable (eu.davidea.flexibleadapter.items.IExpandable)1 ExpandableHeaderItem (eu.davidea.samples.flexibleadapter.items.ExpandableHeaderItem)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1