我有两个类, PersonAdapter 和 AnimalAdapter 。
这两个类几乎完全相同,所以我试图减少尽可能多的锅炉板代码。
PersonAdapter 从 RecyclerView.Adapter 延伸。
AnimalAdapter 从 RecyclerView.Adapter 扩展而来。 这两个类都实现了Filterable接口。
PersonAdapter 定义从 过滤器 扩展的内部类 PersonFilter 。
AnimalAdapter 定义从 过滤器 扩展的内部类 AnimalFilter 。
我想用某种泛型类替换 PersonFilter 和 AnimalFilter ,但我不知道如何将这两个类之间的差异:
PersonFilter
- works with a List<Person>
- uses Person#getName() to make a string comparison
AnimalFilter
- works with a List<Animal>
- uses Animal#getType() to make a string comparison
请快速查看两个课程:
PersonAdapter
public class PersonAdapter extends RecyclerView.Adapter<PersonViewHolder> implements Filterable {
private List<Person> persons;
private PersonFilter personFilter;
public PersonAdapter(List<Person> persons) {
this.persons = persons;
}
...
@Override
public Filter getFilter() {
if(personFilter == null)
personFilter = new PersonFilter(this, persons);
return personFilter;
}
public static class PersonFilter extends Filter {
private final PersonAdapter adapter;
private final List<Person> originalList;
private final List<Person> filteredList;
private PersonFilter(PersonAdapter adapter, List<Person> originalList) {
super();
this.adapter = adapter;
this.originalList = new LinkedList<>(originalList);
this.filteredList = new ArrayList<>();
}
@Override
protected FilterResults performFiltering(CharSequence constraint) {
filteredList.clear();
final FilterResults results = new FilterResults();
if (constraint.length() == 0) {
filteredList.addAll(originalList);
} else {
final String filterPattern = constraint.toString().toLowerCase().trim();
for (final Person person : originalList) {
if (person.getName().toLowerCase().contains(filterPattern.toLowerCase())) {
filteredList.add(person);
}
}
}
results.values = filteredList;
results.count = filteredList.size();
return results;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
adapter.persons.clear();
adapter.persons.addAll((ArrayList<Person>) results.values);
adapter.notifyDataSetChanged();
}
}
}
AnimalAdapter
public class AnimalAdapter extends RecyclerView.Adapter<AnimalViewHolder> implements Filterable {
private List<Animal> animals;
private AnimalFilter animalFilter;
public AnimalAdapter(List<Animal> animals) {
this.animals = animals;
}
...
public static class AnimalFilter extends Filter {
private final AnimalAdapter adapter;
private final List<Animal> originalList;
private final List<Animal> filteredList;
private AnimalFilter(AnimalAdapter adapter, List<Animal> originalList) {
super();
this.adapter = adapter;
this.originalList = new LinkedList<>(originalList);
this.filteredList = new ArrayList<>();
}
@Override
protected FilterResults performFiltering(CharSequence constraint) {
filteredList.clear();
final FilterResults results = new FilterResults();
if (constraint.length() == 0) {
filteredList.addAll(originalList);
} else {
final String filterPattern = constraint.toString().toLowerCase().trim();
for (final Animal animal : originalList) {
if (animal.getType().toLowerCase().contains(filterPattern.toLowerCase())) {
filteredList.add(animal);
}
}
}
results.values = filteredList;
results.count = filteredList.size();
return results;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
adapter.animals.clear();
adapter.animals.addAll((ArrayList<Animal>) results.values);
adapter.notifyDataSetChanged();
}
}
}
我应该如何创建一个与Animal和Person对象一起使用的Filter的通用子类,并且可以由PersonAdapter和AnimalAdapter使用?
答案 0 :(得分:2)
这是一个过滤器,它使用泛型和谓词从问题中提取出两个过滤器。可以使用类似于Guava Iterable.filter()的方法进一步减少代码。
MyAdapterInterface需要是适配器的接口或共享父类,这些适配器共享此过滤器需要调用的所有适配器方法。
public class MyFilter<A extends MyAdapterInterface,T> extends Filter {
private final A adapter;
private final Predicate<T> comparator; // Java 8 class - can easily be created by hand if you are using pre Java 8
// see https://docs.oracle.com/javase/8/docs/api/java/util/function/Predicate.html
private final List<T> originalList;
private final List<T> filteredList;
private MyFilter(A adapter, List<T> originalList, Predicate<T> predicate) {
super();
this.adapter = adapter;
this.comparator = comparator;
this.originalList = new LinkedList<>(originalList);
this.filteredList = new ArrayList<>();
}
@Override
protected FilterResults performFiltering(CharSequence constraint) {
filteredList.clear();
final FilterResults results = new FilterResults();
if (constraint.length() == 0) {
filteredList.addAll(originalList);
} else {
final String filterPattern = constraint.toString().toLowerCase().trim();
for (final T animal : originalList) {
if (predicate.test(animal)) {
filteredList.add(animal);
}
}
}
results.values = filteredList;
results.count = filteredList.size();
return results;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
adapter.clearResults(); // method to be added to MyAdapterInterface
adapter.addResults(results.values); // method to be added to MyAdapterInterface
adapter.notifyDataSetChanged();
}
}