Skip to content

Commit

Permalink
Added possibility to filter in the Header item of a section AND to ke…
Browse files Browse the repository at this point in the history
…ep header also if a subItem has been filtered #73.
  • Loading branch information
davideas committed Apr 25, 2016
1 parent 98a29b2 commit 491e6af
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import eu.davidea.flexibleadapter.FlexibleAdapter;
import eu.davidea.flexibleadapter.items.AbstractHeaderItem;
import eu.davidea.flexibleadapter.items.IFilterable;
import eu.davidea.flexibleadapter.items.ISectionable;
import eu.davidea.samples.flexibleadapter.R;
import eu.davidea.viewholders.FlexibleViewHolder;
Expand All @@ -19,7 +20,7 @@
* <p>A Section should not contain others Sections!</p>
* Headers are not Sectionable!
*/
public class HeaderItem extends AbstractHeaderItem<HeaderItem.HeaderViewHolder> {
public class HeaderItem extends AbstractHeaderItem<HeaderItem.HeaderViewHolder> implements IFilterable {

private static final long serialVersionUID = -7408637077727563374L;

Expand Down Expand Up @@ -89,6 +90,11 @@ public void bindViewHolder(FlexibleAdapter adapter, HeaderViewHolder holder, int
holder.mSubtitle.setText(subTitle);
}

@Override
public boolean filter(String constraint) {
return getTitle() != null && getTitle().toLowerCase().trim().contains(constraint);
}

static class HeaderViewHolder extends FlexibleViewHolder {

public TextView mTitle;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2410,12 +2410,21 @@ public synchronized void filterItems(@NonNull List<T> unfilteredItems) {
if (hasSearchText()) {
int newOriginalPosition = -1;
for (T item : unfilteredItems) {
//Check header first
T header = (T) getHeaderOf(item);
if (header != null && filterObject(header, getSearchText())
&& !values.contains(getHeaderOf(item))) {
values.add(header);
}
if (filterExpandableObject(item, getSearchText())) {
RestoreInfo restoreInfo = getPendingRemovedItem(item);
if (restoreInfo != null) {
//If found point to the new reference while filtering
restoreInfo.filterRefItem = ++newOriginalPosition < values.size() ? values.get(newOriginalPosition) : null;
} else {
if (hasHeader(item) && !values.contains(getHeaderOf(item))) {
values.add((T) getHeaderOf(item));
}
values.add(item);
newOriginalPosition++;
if (isExpandable(item)) {
Expand Down

0 comments on commit 491e6af

Please sign in to comment.