您可以使用超类适配器,并将其扩展以用于不同情况。这使得编写更少的代码和重用超类中的东西成为可能。
这里是我的项目一个例子(超级抽象RecyclerViewAdapter带过滤器的可能性):
public abstract class GenericRecycleAdapter<T, K extends Holders.TextImageHolder> extends RecyclerView.Adapter implements Filterable {
private final Context context;
protected List<T> mList = new ArrayList<T>() {
};
private List<T> originalData = null;
private Action1<List> task;
private ItemFilter mFilter = new ItemFilter();
private int lastPosition;
public GenericRecycleAdapter(List<T> list, Context context) {
this.context = context;
if (list != null) {
this.mList = list;
}
this.originalData = list;
}
public void refresh(List<T> list) {
if (list != null) {
this.mList = list;
}
this.originalData = list;
notifyDataSetChanged();
}
public T getItem(int position) {
return mList.get(position);
}
public Context getContext() {
return context;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(getLayout(), parent, false);
return getCustomHolder(v);
}
public Holders.TextImageHolder getCustomHolder(View v) {
return new Holders.TextImageHolder(v) {
@Override
public void onClick(View v) {
onItem(mList.get(this.getAdapterPosition()));
onPosition(this.getAdapterPosition());
}
};
}
protected void onPosition(int position) {
}
protected abstract void onItem(T t);
public abstract int getLayout();
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
onSet(mList.get(position), (K) holder);
onSetPosition((K) holder, position);
setAnimation(((K) holder).getContainer(), position);
}
protected void onSetPosition(K holder, int position) {
}
public abstract void onSet(T item, K holder);
@Override
public int getItemCount() {
return mList.size();
}
public void addObserverToFilter(Action1<List> task) {
this.task = task;
}
@Override
public Filter getFilter() {
return mFilter;
}
private class ItemFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
String filterString = constraint.toString().toLowerCase();
FilterResults results = new FilterResults();
final List<T> list = originalData;
int count = list.size();
final ArrayList<T> nlist = new ArrayList<>(count);
String filterableString;
for (int i = 0; i < count; i++) {
filterableString = list.get(i).toString();
if (filterImplementation(filterString, filterableString.toLowerCase())) {
nlist.add(list.get(i));
}
}
results.values = nlist;
results.count = nlist.size();
return results;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
mList = (ArrayList<T>) results.values;
notifyDataSetChanged();
if (task != null) {
task.call(mList);
}
}
}
public List<T> getFilteredList() {
return mList;
}
public List<T> getOriginalList() {
return originalData;
}
protected boolean filterImplementation(String key, String door) {
return door.startsWith(key);
}
protected void setAnimation(View viewToAnimate, int position)
{
if (viewToAnimate == null) {
return;
}
if (position > lastPosition) {
Animation animation = AnimationUtils.loadAnimation(context, android.R.anim.slide_in_left);
viewToAnimate.startAnimation(animation);
lastPosition = position;
}
}
}
和处理程序:对另一观点
public class Holders {
public static class TextImageHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private final View container;
public TextImageHolder(View itemView) {
super(itemView);
container = itemView.findViewById(R.id.container);
//add other view
container.setOnClickListener(this);
}
@Override
public void onClick(View v) {
}
}
}
用例:
public class ChildAdapter extends GenericRecycleAdapter<String,Holders.TextImageHolder> {
public ChildAdapter (List<String> list, Context context) {
super(list, context);
}
@Override
protected void onItem(String s) {
//triggers when container is clicked
}
@Override
public int getLayout() {
return R.layout.some_row; //the id's here should be the same as in the Handler
}
@Override
public void onSet(String item, Holders.TextImageHolder holder) {
holder.text.setText(item);
}
}
我认为单身适配器与“属于检查”..但正如你所说..它是讨厌.. :) 我会制作其他适配器。 我会尽快将其标记为答案。 –
@CodingJohn不错的选择。同样在这种情况下,如果其中一个列表的功能应该改变,你不必担心其他东西会被破坏 - 因为它们都有自己的适配器 –
是的,这是真的。而且它更容易重用一些代码,如果你已经这样做了:) –