Support stable ids in chip adapters
This commit is contained in:
@@ -40,6 +40,11 @@ public class EncryptRecipientChipsInput extends ChipsInput<EncryptRecipientChip>
|
||||
this.keyInfo = keyInfo;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getId() {
|
||||
return keyInfo.master_key_id();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isKeptForConstraint(CharSequence constraint) {
|
||||
return keyInfo.uidSearchString().contains(constraint);
|
||||
|
||||
@@ -45,6 +45,7 @@ public abstract class ChipsAdapter<T extends FilterableItem, VH extends Recycler
|
||||
this.context = context;
|
||||
|
||||
this.hintLabel = chipsInput.getHint();
|
||||
setHasStableIds(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -67,7 +68,12 @@ public abstract class ChipsAdapter<T extends FilterableItem, VH extends Recycler
|
||||
|
||||
@Override
|
||||
public long getItemId(int position) {
|
||||
return chipList.get(position).hashCode();
|
||||
if (position == chipList.size()) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
FilterableItem item = getItem(position);
|
||||
return item != null ? item.getId() : RecyclerView.NO_ID;
|
||||
}
|
||||
|
||||
private void autofitEditText() {
|
||||
|
||||
@@ -19,6 +19,8 @@ public abstract class FilterableAdapter<T extends FilterableItem, VH extends Rec
|
||||
public FilterableAdapter(List<? extends T> itemList) {
|
||||
itemFilter = new ItemFilter(itemList);
|
||||
displayedList.addAll(itemList);
|
||||
|
||||
setHasStableIds(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -35,6 +37,12 @@ public abstract class FilterableAdapter<T extends FilterableItem, VH extends Rec
|
||||
return itemFilter;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getItemId(int position) {
|
||||
FilterableItem item = getItem(position);
|
||||
return item != null ? item.getId() : RecyclerView.NO_ID;
|
||||
}
|
||||
|
||||
private class ItemFilter extends Filter {
|
||||
private List<T> originalList;
|
||||
private List<T> filteredList;
|
||||
@@ -86,6 +94,7 @@ public abstract class FilterableAdapter<T extends FilterableItem, VH extends Rec
|
||||
}
|
||||
|
||||
public interface FilterableItem {
|
||||
long getId();
|
||||
boolean isKeptForConstraint(CharSequence constraint);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,7 +5,6 @@ import com.pchmn.materialchips.adapter.FilterableAdapter.FilterableItem;
|
||||
|
||||
|
||||
public interface ChipInterface extends FilterableItem {
|
||||
Object getId();
|
||||
String getLabel();
|
||||
String getInfo();
|
||||
}
|
||||
|
||||
@@ -4,17 +4,16 @@ package com.pchmn.materialchips.simple;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
|
||||
import com.pchmn.materialchips.adapter.FilterableAdapter.FilterableItem;
|
||||
import com.pchmn.materialchips.model.ChipInterface;
|
||||
|
||||
|
||||
public class SimpleChip implements ChipInterface {
|
||||
private Object id;
|
||||
private long id;
|
||||
private String label;
|
||||
private String info;
|
||||
private String filterString;
|
||||
|
||||
public SimpleChip(@NonNull Object id, @NonNull String label, @Nullable String info, @Nullable String filterString) {
|
||||
public SimpleChip(@NonNull long id, @NonNull String label, @Nullable String info, @Nullable String filterString) {
|
||||
this.id = id;
|
||||
this.label = label;
|
||||
this.info = info;
|
||||
@@ -24,10 +23,11 @@ public class SimpleChip implements ChipInterface {
|
||||
public SimpleChip(@NonNull String label, @Nullable String info) {
|
||||
this.label = label;
|
||||
this.info = info;
|
||||
this.id = (label + info).hashCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getId() {
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user