我在我的项目中使用DataBindings和RecyclerView。 我有一个RecyclerView的基础适配器。它看起来像这样:
public abstract class BaseAdapter<T extends ViewDataBinding> extends RecyclerView.Adapter<BaseAdapter.ViewHolder> {
public BaseAdapter() {}
public class ViewHolder extends RecyclerView.ViewHolder {
public T binding;
public ViewHolder(View view) {
super(view);
binding = DataBindingUtil.bind(view);
}
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
... code ...
}
}
ViewHolder 扩展类仅在绑定字段类型上有所不同。实现扩展 BaseAdapter 类后:
public class BaseAdapterExtended extends BaseAdapter<BaseAdapterExtendedBinding> {
public BaseAdapterExtended(ArrayList<ItemModel> itemModels) {
super();
mData = itemModels;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
return new ViewHolder(BaseAdapterExtendedBinding.inflate(inflater, parent, false).getRoot());
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
super.onBindViewHolder(holder, position);
}
@Override
protected View getItemRootView(ViewHolder holder) {
return holder.binding.item;
}
@Override
public int getItemCount() {
return mData.size();
}
}
我收到了下一个编译错误:
我遇到了下一个编译错误:
error: BaseAdapterExtended is not abstract and does not override abstract method onBindViewHolder(BaseAdapter.ViewHolder,int) in Adapter
似乎BaseAdapterExtended没有这个方法,但它确实存在。 如果我改变
public void onBindViewHolder(ViewHolder holder, int position)
to
public void onBindViewHolder(BaseAdapter.ViewHolder holder, int position)
投影编译成功,但类型绑定将是 ViewDataBinding 而不是 BaseAdapterExtendedBinding。为什么会这样?有什么想法吗?
BaseAdapterExtended
中,ViewHolder 的导入是什么意思?为什么在BaseAdapterExtended
中需要一个名为GamesListAdapter
的构造函数? - Blackbelt