public abstract class BaseViewHolder<T> extends RecyclerView.ViewHolder {
private SparseArray<View> mViews;
private View mItemView;
public BaseViewHolder(View itemView) {
super(itemView);
mItemView = itemView;
mViews = new SparseArray<>();
}
public View getView(int resId) {
View view = mViews.get(resId);
if (view == null) {
view = mItemView.findViewById(resId);
mViews.put(resId, view);
}
return view;
}
public abstract void bindViewData(T data);
}
public class MultiRecyclerAdapter extends RecyclerView.Adapter<BaseViewHolder> {
private List<TypeData> mData;
private TypeFactory mTypeFactory;
public MultiRecyclerAdapter(List<TypeData> data) {
mData = data;
mTypeFactory = new MultiTypeFactory();
}
public List<TypeData> getData() {
return mData;
}
public void setData(List<TypeData> data) {
mData = data;
notifyDataSetChanged();
}
@NonNull
@Override
public BaseViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(viewType, parent, false);
return mTypeFactory.createViewHolder(viewType, view);
}
@Override
public void onBindViewHolder(@NonNull BaseViewHolder holder, int position) {
holder.bindViewData(mData.get(position));
}
@Override
public int getItemViewType(int position) {
return mData.get(position).type(mTypeFactory);
}
@Override
public int getItemCount() {
return (mData != null ? mData.size() : 0);
}
}
public interface TypeFactory {
int type(NewsEntry.NewsDataBean newsDataBean);
int type(EmptyBean emptyBean);
int type(NoMoreEntity noMoreEntity);
BaseViewHolder createViewHolder(int type, View itemView);
}
public class MultiTypeFactory implements TypeFactory {
private final int NEWS_ITEM_LAYOUT_ID = R.layout.rv_news_item;
private final int VIDEO_ITEM_LAYOUT_ID = R.layout.rv_video_item;
private final int EMPTY_ITEM_LAYOUT_ID = R.layout.rv_empty_item;
private final int NO_MORE_ITEM_LAYOUT_ID = R.layout.rv_no_more_item;
@Override
public int type(NewsEntry.NewsDataBean newsDataBean) {
return NEWS_ITEM_LAYOUT_ID;
}
@Override
public int type(EmptyBean emptyBean) {
return EMPTY_ITEM_LAYOUT_ID;
}
@Override
public int type(NoMoreEntity noMoreEntity) {
return NO_MORE_ITEM_LAYOUT_ID;
}
@Override
public BaseViewHolder createViewHolder(int type, View itemView) {
switch (type) {
case NEWS_ITEM_LAYOUT_ID:
return new NewsViewHolder(itemView);
case EMPTY_ITEM_LAYOUT_ID:
return new EmptyViewHolder(itemView);
case NO_MORE_ITEM_LAYOUT_ID:
return new NoMoreViewHolder(itemView);
default:
throw new IllegalStateException("incorrect layout type");
}
}
}
Как мне избавиться от предупреждения:
Непроверенный вызов 'bindViewData (T)' как члена типа raw
'com.kunlun.xianshi.viewholder.BaseViewHolder' меньше ... (Ctrl + F1)
Сигналы мест, где компилятором выдается непроверенное предупреждение,
например:
void f (карта HashMap) {
map.put («ключ», «значение»); } Подсказка: Pass -Xlint: снят с javac для получения более подробной информации.
, который находится в следующем коде:
@Override
public void onBindViewHolder(@NonNull BaseViewHolder holder, int position) {
holder.bindViewData(mData.get(position));
}
Ниже описано, как я могу его использовать:
public class NewsViewHolder extends BaseViewHolder<NewsEntry.NewsDataBean> {
public NewsViewHolder(View itemView) {
super(itemView);
}
@Override
public void bindViewData(final NewsEntry.NewsDataBean data) {
TextView tvTitle = (TextView) getView(R.id.tv_news_title);
TextView tvAuthor = (TextView) getView(R.id.tv_news_author);
TextView tvDate = (TextView) getView(R.id.tv_news_date);
ImageView imageView = (ImageView) getView(R.id.iv_news_pic1);
tvTitle.setText(data.getTitle());
tvAuthor.setText("");
tvDate.setText(DateUtils.getDateStr(null,data.getTime()));
String picPath = data.getCover();
if (picPath != null) {
RequestOptions options = new RequestOptions()
.placeholder(R.drawable.empty_data);
Glide.with(itemView.getContext()).load(picPath).apply(options).into(imageView);
} else {
imageView.setVisibility(GONE);
}
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(itemView.getContext(), WebActivity.class);
intent.putExtra("URL", data.getUrl());
itemView.getContext().startActivity(intent);
}
});
}
}
public class NewsEntry {
public static class NewsDataBean implements TypeData {
int id;
private String title;
int categoryId;
String url;
String cover;
long time;
public String getTitle() {
return title;
}
public int getCategoryId() {
return categoryId;
}
public String getUrl() {
return url;
}
public long getTime() {
return time;
}
public int getId() {
return id;
}
public String getCover() {
return cover;
}
@Override
public int type(TypeFactory typeFactory) {
return typeFactory.type(this);
}
}
private List<NewsDataBean> news;
public List<NewsDataBean> getNews() {
return news;
}
}