我有一个BaseAdapter
类,我正在为扩展BaseAdapter的所有Adapter类中的列表数据实现“刷新”,如下所示:
RecyclerView.Adapter adapter = mRecyclerView.getAdapter();
if (adapter instanceof "Adapter Class that extends BaseAdapter") {
((Adapter Class that extends BaseAdapter") adapter).refreshData(data);
} else {
adapter = new Adapter Class that extends BaseAdapter"();
(("Adapter Class that extends BaseAdapter") adapter).refreshData(topicDetails);
mRecyclerView.setAdapter(adapter);
}
其中refreshData(T data)
是BaseAdapter
中实现notifyDataChanged
等的通用抽象方法。
因此,在扩展BaseAdapter
的clss中,我可以调用:
public class MyAdapter extends BaseAdapter<Data> {
@Override
public void refreshData(Data data) {
insertItems(data);
notifyDataSetChanged();
}
}
在活动/片段或者RecyclerView的任何地方:
mRecyclerView.refreshData(mAdapter)
或其他什么。我不确定如何从BaseAdapter中获取“扩展BaseAdapter的适配器类”
tldr
基本上我想写一个通用的方法:
RecyclerView.Adapter adapter = mRecyclerView.getAdapter();
if (adapter instanceof "Adapter Class that extends BaseAdapter") {
((Adapter Class that extends BaseAdapter") adapter).refreshData(data);
} else {
adapter = new Adapter Class that extends BaseAdapter"();
(("Adapter Class that extends BaseAdapter") adapter).refreshData(topicDetails);
mRecyclerView.setAdapter(adapter);
}
所以我不必为每个List
执行此操作答案 0 :(得分:0)
首先,此案例与您使用的继承有关。您可以在此处使用通用继承:
RecyclerView.Adapter adapter = mRecyclerView.getAdapter();
if (adapter instanceof BaseAdapter) {
((BaseAdapter) adapter).refreshData(topicDetails); //You will get unchecked call here but it won't cause a problem
} else {
adapter = new BaseAdapter<>();
((BaseAdapter) adapter).refreshData(topicDetails); //You will get unchecked call error here but it won't cause a problem
mRecyclerView.setAdapter(adapter);
}
答案 1 :(得分:0)
这是我的BaseAdapter类。
/* general recyclerview adapter */
public abstract class BaseRecyclerAdapter<T, VH extends RecyclerView.ViewHolder> extends RecyclerView.Adapter<VH> {
public final List<T> itemList;
public OnItemClickListener onItemClickListener;
public BaseRecyclerAdapter(List<T> itemList, OnItemClickListener onItemClickListener) {
this.itemList = itemList;
this.onItemClickListener = onItemClickListener;
}
public BaseRecyclerAdapter(List<T> itemList) {
this.itemList = itemList;
}
public void clear() {
itemList.clear();
notifyDataSetChanged();
}
public void add(List<T> itemListToAdd) {
itemList.addAll(itemListToAdd);
notifyDataSetChanged();
}
@Override
public int getItemCount() {
return itemList.size();
}
public interface OnItemClickListener<Object> {
void onItemClick(View view, Object object);
}
}
并像
一样使用它public class AdapterDemo extends BaseRecyclerAdapter<BeanCart,AdapterDemo.MyViewHolder> {
public AdapterDemo(List<BeanCart> itemList) {
super(itemList);
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return null;
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
}
class MyViewHolder extends RecyclerView.ViewHolder {
MyViewHolder(View itemView) {
super(itemView);
}
}
}
在这里你将传递两个类类型
T&GT;清单类型
VH&GT;查看持有人
喜欢这个public class AdapterDemo extends BaseRecyclerAdapter<BeanCart,AdapterDemo.MyViewHolder>