Search in sources :

Example 11 with IHeader

use of eu.davidea.flexibleadapter.items.IHeader in project Neuronizer by djuelg.

the class TodoListFragment method onDestroyActionMode.

@Override
public void onDestroyActionMode(ActionMode mode) {
    changeAppbarColor(getActivity(), R.color.colorPrimary);
    for (IHeader header : mAdapter.getHeaderItems()) {
        TodoListHeaderViewModel vm = (TodoListHeaderViewModel) header;
        boolean shouldExpand = vm.getHeader().isExpanded();
        if (shouldExpand && !omitActionModeExpansion)
            mAdapter.expand(vm);
    }
}
Also used : IHeader(eu.davidea.flexibleadapter.items.IHeader) TodoListHeaderViewModel(de.djuelg.neuronizer.presentation.ui.flexibleadapter.TodoListHeaderViewModel)

Example 12 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 13 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;
        // 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)

Example 14 with IHeader

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

the class FlexibleAdapter method filterObject.

/**
 * This method is a wrapper filter for expandable items.<br>
 * It performs filtering on the subItems returning true, if the any child should be in the
 * filtered collection.
 * <p>If the provided item is not an expandable it will be filtered as usual by
 * {@link #filterObject(T, Serializable)}.</p>
 *
 * @param item the object with subItems to be inspected
 * @return true, if the object should be in the filteredResult, false otherwise
 * @since 5.0.0-b1
 */
private boolean filterObject(T item, List<T> values) {
    // Stop filter task if cancelled
    if (mFilterAsyncTask != null && mFilterAsyncTask.isCancelled()) {
        return false;
    }
    // Skip already filtered items (it happens when internal originalList)
    if (mOriginalList != null && (isScrollableHeaderOrFooter(item) || values.contains(item))) {
        return false;
    }
    // Start to compose the filteredItems to maintain the order of addition
    // It will be discarded if no subItem will be filtered
    List<T> filteredItems = new ArrayList<>();
    filteredItems.add(item);
    // Filter subItems
    boolean filtered = filterExpandableObject(item, filteredItems);
    // If no subItem was filtered, fallback to Normal filter
    if (!filtered) {
        filtered = filterObject(item, getFilter(Serializable.class));
    }
    if (filtered) {
        // Check if header has to be added too
        IHeader header = getHeaderOf(item);
        if (headersShown && hasHeader(item) && !values.contains(header)) {
            header.setHidden(false);
            values.add((T) header);
        }
        values.addAll(filteredItems);
    }
    item.setHidden(!filtered);
    return filtered;
}
Also used : IHeader(eu.davidea.flexibleadapter.items.IHeader)

Example 15 with IHeader

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

the class FlexibleAdapter method prepareItemsForUpdate.

private void prepareItemsForUpdate(List<T> newItems) {
    // Clear cache of bound view holders
    if (notifyChangeOfUnfilteredItems) {
        discardBoundViewHolders();
    }
    // Display Scrollable Headers and Footers
    restoreScrollableHeadersAndFooters(newItems);
    int position = 0;
    IHeader sameHeader = null;
    // to optimize the operations of adding hidden items/subItems
    while (position < newItems.size()) {
        T item = newItems.get(position);
        // Expand Expandable
        if (isExpanded(item)) {
            IExpandable expandable = (IExpandable) item;
            expandable.setExpanded(true);
            List<T> subItems = getExpandableList(expandable, false);
            int itemCount = newItems.size();
            if (position < itemCount) {
                newItems.addAll(position + 1, subItems);
            } else {
                newItems.addAll(subItems);
            }
        }
        // Display headers too
        if (!headersShown && isHeader(item) && !item.isHidden()) {
            headersShown = true;
        }
        IHeader header = getHeaderOf(item);
        if (header != null && !header.equals(sameHeader) && !isExpandable((T) header)) {
            header.setHidden(false);
            sameHeader = header;
            newItems.add(position, (T) header);
            position++;
        }
        position++;
    }
}
Also used : IHeader(eu.davidea.flexibleadapter.items.IHeader) IExpandable(eu.davidea.flexibleadapter.items.IExpandable) SuppressLint(android.annotation.SuppressLint)

Aggregations

IHeader (eu.davidea.flexibleadapter.items.IHeader)21 SuppressLint (android.annotation.SuppressLint)8 ISectionable (eu.davidea.flexibleadapter.items.ISectionable)8 Test (org.junit.Test)4 AbstractFlexibleItem (eu.davidea.flexibleadapter.items.AbstractFlexibleItem)3 IExpandable (eu.davidea.flexibleadapter.items.IExpandable)3 TodoListHeaderViewModel (de.djuelg.neuronizer.presentation.ui.flexibleadapter.TodoListHeaderViewModel)2 ArrayList (java.util.ArrayList)2 TodoListHeader (de.djuelg.neuronizer.domain.model.todolist.TodoListHeader)1 IFlexible (eu.davidea.flexibleadapter.items.IFlexible)1 AbstractItem (eu.davidea.samples.flexibleadapter.items.AbstractItem)1 ExpandableHeaderItem (eu.davidea.samples.flexibleadapter.items.ExpandableHeaderItem)1 HeaderItem (eu.davidea.samples.flexibleadapter.items.HeaderItem)1 InstagramHeaderItem (eu.davidea.samples.flexibleadapter.items.InstagramHeaderItem)1 StaggeredHeaderItem (eu.davidea.samples.flexibleadapter.items.StaggeredHeaderItem)1 List (java.util.List)1