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);
}
}
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));
}
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);
}
});
}
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;
}
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++;
}
}
Aggregations