在写如下 RecyclerViewAdapter 时会报错 未覆盖Adapter中的抽象方法onBindViewHolder 。但是,明明已经覆盖实现了,怎么又保存呢。把 CommItemHolder 提出去单独搞一个类就不会有问题。
public class CommRecyclerViewAdapter<T> extends RecyclerView.Adapter<CommRecyclerViewAdapter.CommItemHolder> {
private List<T> mData;
private AdapterItem mAdapterItem;
private View.OnClickListener mOnClickListener;
private View.OnLongClickListener mOnLongClickListener;
public CommRecyclerViewAdapter(List<T> mData, AdapterItem mAdapterItem) {
this.mData = mData;
this.mAdapterItem = mAdapterItem;
}
...
@NonNull
@Override
public CommItemHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new CommItemHolder(parent.getContext(), parent, mAdapterItem);
}
@Override
public void onBindViewHolder(@NonNull CommItemHolder holder, int position) {
holder.itemView.setTag(position);
holder.itemView.setOnClickListener(mOnClickListener);
holder.itemView.setOnLongClickListener(mOnLongClickListener);
holder.mAdapterItem.handleData(position);
}
@Override
public int getItemCount() {
return mData == null ? 0 : mData.size();
}
public List<T> getData() {
return mData;
}
class CommItemHolder extends RecyclerView.ViewHolder {
AdapterItem mAdapterItem;
CommItemHolder(Context context, ViewGroup parent, AdapterItem adapterItem) {
super(LayoutInflater.from(context).inflate(adapterItem.getResId(), parent, false));
mAdapterItem = adapterItem;
mAdapterItem.bindViews(this.itemView);
}
}
public interface AdapterItem {
void handleData(int position);
int getResId();
void bindViews(View itemView);
}
}
网友评论