为什么我出现此错误?
java.lang.ClassCastException: com.example.pc.mytest.MainItemsRCVAdapter$ViewHolderHeader cannot be cast to com.example.pc.mytest.MainItemsRCVAdapter$ViewHolder
我正在尝试使用Recycler一个标题和一个水平标题。
这是班级:
public class MainItemsRCVAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private ArrayList<ListMainItem> mainItems;
private Context mContext;
private static final int TYPE_HEADER = 0;
private static final int TYPE_ITEM = 1;
public MainItemsRCVAdapter(Context context,ArrayList<ListMainItem> maindata) {
setMaindata(maindata);
this.mainItems = maindata;
this.mContext = context;
}
public void setMaindata(ArrayList<ListMainItem> maindatahorizon) {
this.mainItems = maindatahorizon;
}
public ArrayList<ListMainItem> getMaindata() {
return mainItems;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
if (viewType == TYPE_ITEM) {
//inflate your layout and pass it to view holder
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.main_adapter_horizontal, viewGroup, false);
return new ViewHolder(v);
} else if (viewType == TYPE_HEADER) {
//inflate your layout and pass it to view holder
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.main_item_adapter, viewGroup, false);
return new ViewHolderHeader(v);
}
throw new RuntimeException("there is no type that matches the type " + viewType + " + make sure your using types correctly");
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int i) {
if (holder instanceof ViewHolder) {
// String name = getDataVertical().get(position-1).getImages();
String name =mainItems.get(i).geImgName();
String urlimage =mainItems.get(i).getImgUrl();
((ViewHolder) holder).tv_items.setText(mainItems.get(i).geImgName());
// holder.tv_items.setText(mainItems.get(i).geImgName());
Log.d("###", "Setting name: " + name);
Picasso.with(mContext)
.load(R.drawable.upload).resize(240, 120)
.into(((ViewHolder) holder).img_items);
Log.d("###", "Setting url: " + urlimage);
} else if (holder instanceof ViewHolderHeader) {
//cast holder to VHHeader and set data for header.
((ViewHolder) holder).tv_items.setText(mainItems.get(i).geImgName());
Log.d("####", "HEADER");
}
//
// viewHolder.tv_items.setText(mainItems.get(i).geImgName());
// Picasso.with(mContext)
// .load(mainItems.get(i)
// .getImgUrl()).resize(240, 120)
// .into(viewHolder.img_items);
}
@Override
public int getItemCount() {
return mainItems.size();
}
@Override
public int getItemViewType(int position) {
if (isPositionHeader(position)) {
return TYPE_HEADER;
}
return TYPE_ITEM;
}
private boolean isPositionHeader(int position) {
return position == 0;
}
public class ViewHolder extends RecyclerView.ViewHolder{
private TextView tv_items;
private ImageView img_items;
private ImageView mDataImgView;
public ViewHolder(View view) {
super(view);
tv_items = (TextView)view.findViewById(R.id.tv_items_horz);
img_items = (ImageView) view.findViewById(R.id.img_items_horz);
}
public ImageView getmDataImgView()
{
return mDataImgView;
}
}
public class ViewHolderHeader extends RecyclerView.ViewHolder{
private final RecyclerView mHorizontalRecyclerView;
public ViewHolderHeader(View v) {
super(v);
mHorizontalRecyclerView = (RecyclerView) v.findViewById(R.id.recycler_view_horizontal);
MainItemsRCVAdapterHorizontal mAdapter = new MainItemsRCVAdapterHorizontal(getMaindata(), mContext);
LinearLayoutManager layoutManager
= new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false);
mHorizontalRecyclerView.setLayoutManager(layoutManager);
mHorizontalRecyclerView.setAdapter(mAdapter);
}
}
}
这是横向
public class MainItemsRCVAdapterHorizontal extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private final ArrayList<ListMainItem> mData;
private final Context mContext;
public MainItemsRCVAdapterHorizontal(ArrayList<ListMainItem> dataHorizontal,Context context) {
mData = dataHorizontal;
mContext=context;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.main_adapter_horizontal, parent, false);
return new MainItemsRCVAdapterHorizontal.MyViewHolderHeader(v);
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
String name = mData.get(position).geImgName();
Log.d("###", "Setting name: " + name);
// ((RecyclerViewAdapterHorizontal.MyViewHolderHeader) holder).getmDataTextView().setText(name);
((MainItemsRCVAdapterHorizontal.MyViewHolderHeader) holder).getmDataImgView().setImageResource(mContext.getResources().getIdentifier(name, "drawable", "com.example.mohamadmouazen.lebgame.Adapters"));
}
@Override
public int getItemCount() {
return mData.size();
}
private class MyViewHolderHeader extends RecyclerView.ViewHolder {
// private final TextView mDataTextView;
private final ImageView mDataImgView;
public MyViewHolderHeader(View v) {
super(v);
// mDataTextView = (TextView) v.findViewById(R.id.data_vertical);
mDataImgView = (ImageView) v.findViewById(R.id.img_items_horz);
}
public ImageView getmDataImgView() {
return mDataImgView;
}
/* public TextView getmDataTextView() {
return mDataTextView;
}*/
}
}
答案 0 :(得分:2)
因为您正在进行无效投射。
检查代码:
} else if (holder instanceof ViewHolderHeader) {
//cast holder to VHHeader and set data for header.
((ViewHolder) holder).tv_items.setText(mainItems.get(i).geImgName());
Log.d("####", "HEADER");
}
您正在将ViewHolderHeader
投射到ViewHolder