java-在onCreateViewHolder中使用不同的视图
作者:互联网
我遵循了Google Developer的YouTube频道的this教程,以实施AdMob原生快速广告.
我收到以下错误:
required: packagename.adapter.viewHolder
found : packagename.adapter.NativeExpressAdViewHolder
这是我的onCreateViewHolder的样子:
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
switch (viewType) {
case AD_VIEW_TYPE:
View nativeExpressLayoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.native_express_add_container, parent, false);
return new NativeExpressAdViewHolder(nativeExpressLayoutView);
case MENU_ITEM_VIEW_TYPE:
default:
View myLayoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list, parent, false);
return new ViewHolder(myLayoutView);
}
}
这是我的2个不同的ViewHolder类:
class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
ViewHolder(View itemView) {
super(itemView);
}
}
public class NativeExpressAdViewHolder extends RecyclerView.ViewHolder {
NativeExpressAdViewHolder(View view) {
super(view);
}
}
这是没有答案的类似问题:
> Error “Incompatible types” while adding NativeAds in recyclerView
> How to add NativeExpressAdView with Custom Model List Android?
编辑:
这是我要求的完整适配器:
public class MainActivityVideoAdapter extends Adapter<MainActivityVideoAdapter.ViewHolder> {
ArrayList<Bitmap> bitmapArrayList;
Context context;
LayoutInflater layoutInflater;
View myLayoutView;
ArrayList<PathModel> ThumbPathList;
ArrayList<PathModel> VideoPathList = new ArrayList();
static DBManager manager;
long _id;
private static final int MENU_ITEM_VIEW_TYPE = 0;
private static final int AD_VIEW_TYPE = 1;
class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
//Video Title
TextView videoName;
//Video Image
CircularImageView videoThumb;
//PopupMenu
ImageButton viewholderOtions;
ViewHolder(View itemView) {
super(itemView);
viewholderOtions = (ImageButton) myLayoutView.findViewById(R.id.viewholderOptions);
videoName = (TextView) myLayoutView.findViewById(R.id.FilePath);
videoThumb = (CircularImageView) myLayoutView.findViewById(R.id.VideoThumbnail);
//View onClick
itemView.setOnClickListener(this);
//Popup onClick
viewholderOtions.setOnClickListener(this);
}
//Handling click events
@Override
public void onClick(View v) {
if (v == viewholderOtions) {
int position = (int) v.getTag();
showPopupMenu(viewholderOtions, position);
}
}
}
public class NativeExpressAdViewHolder extends RecyclerView.ViewHolder {
NativeExpressAdViewHolder(View view) {
super(view);
}
}
public MainActivityVideoAdapter(Context context, ArrayList<PathModel> ThumbPathList, ArrayList<PathModel> VideoPathList) {
this.context = context;
this.ThumbPathList = ThumbPathList;
this.VideoPathList = VideoPathList;
}
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
switch (viewType) {
case AD_VIEW_TYPE:
View nativeExpressLayoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.native_express_add_container, parent, false);
return new NativeExpressAdViewHolder(nativeExpressLayoutView);
case MENU_ITEM_VIEW_TYPE:
default:
View myLayoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list, parent, false);
return new ViewHolder(myLayoutView);
}
}
public void onBindViewHolder(final ViewHolder myHolder, final int position) {
int viewType = getItemViewType(position);
switch (viewType) {
case AD_VIEW_TYPE:
case MENU_ITEM_VIEW_TYPE:
default:
final PathModel videoPathModel = this.VideoPathList.get(position);
PathModel thumbathModel = this.ThumbPathList.get(position);
File file = new File(videoPathModel.getPath());
String filename = file.getName();
myHolder.videoName.setText(filename);
myHolder.videoThumb.setImageURI(Uri.parse(thumbathModel.getPath()));
myHolder.viewholderOtions.setTag(position);
myHolder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent= new Intent(context, VideoPlayerActivity.class);
intent.putExtra("fromFA2", "fromFA2");
context.startActivity(intent);
}
});
}
}
解决方法:
您好@ClassA我发现您已经在onBindViewHolder()中导入了本地类而不是RecyclerView.ViewHolder
请检查以下代码,这可能会对您有所帮助.
public class MainActivityVideoAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
/*
------------------
your constructor goes here
-----------------
*/
@Override
public int getItemCount() {
return 0;
}
public void onBindViewHolder(final RecyclerView.ViewHolder myHolder, final int position) {
int viewType = getItemViewType(position);
switch (viewType) {
case AD_VIEW_TYPE:
break;
case MENU_ITEM_VIEW_TYPE:
break;
}
}
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
switch (viewType) {
case AD_VIEW_TYPE:
View nativeExpressLayoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.native_express_add_container, parent, false);
return new NativeExpressAdViewHolder(nativeExpressLayoutView);
case MENU_ITEM_VIEW_TYPE:
View myLayoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list, parent, false);
return new ViewHolder(myLayoutView);
}
}
class ViewHolder extends RecyclerView.ViewHolder {
ViewHolder(View itemView) {
super(itemView);
}
}
public class NativeExpressAdViewHolder extends RecyclerView.ViewHolder {
NativeExpressAdViewHolder(View view) {
super(view);
}
}
}
该代码不包含您的变量和逻辑,该代码可完美导入和使用方法.
如果这样可以解决您的问题,请使该答案得到批准.
编码愉快.
标签:android-viewholder,android-recyclerview,admob,java,android 来源: https://codeday.me/bug/20191110/2014699.html