在 onCreateViewHolder 中使用不同的视图
Using different views in onCreateViewHolder
我按照 Google 开发者 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);
}
});
}
}
我怀疑调用 class ViewHolder
把你搞砸了。称它为 MenuViewHolder
或其他名称,因为现在在这样的方法中简单地编写 ViewHolder
时:
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
它假定您正在考虑自己的 class ViewHolder
,而不是 RecyclerView.ViewHolder
,然后类型不兼容。您可以通过将 class 名称更改为其他名称 或将方法声明更改为 来修复它:
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Although the latter would solve it as well, I'd opt for name changing, cause it's not a good practice to have classes with same names as native ones (e.g. View
, ViewHolder
, BaseAdapter
etc.) cause it might produce confusion as it did here.
使用 RecyclerView 的 ViewHolder,而不是您的 ViewHolder。
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);
}
}
你好@ClassA 我发现你一直在导入本地 class 而不是 RecyclerView.ViewHolder
onBindViewHolder()
请看下面的代码,这可能对你有帮助。
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);
}
}
}
此代码不包含您的变量和逻辑,此代码用于完美导入和使用方法。
如果这解决了您的问题,请批准此答案。
快乐编码。
我按照 Google 开发者 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);
}
});
}
}
我怀疑调用 class ViewHolder
把你搞砸了。称它为 MenuViewHolder
或其他名称,因为现在在这样的方法中简单地编写 ViewHolder
时:
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
它假定您正在考虑自己的 class ViewHolder
,而不是 RecyclerView.ViewHolder
,然后类型不兼容。您可以通过将 class 名称更改为其他名称 或将方法声明更改为 来修复它:
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Although the latter would solve it as well, I'd opt for name changing, cause it's not a good practice to have classes with same names as native ones (e.g.
View
,ViewHolder
,BaseAdapter
etc.) cause it might produce confusion as it did here.
使用 RecyclerView 的 ViewHolder,而不是您的 ViewHolder。
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);
}
}
你好@ClassA 我发现你一直在导入本地 class 而不是 RecyclerView.ViewHolder
onBindViewHolder()
请看下面的代码,这可能对你有帮助。
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);
}
}
}
此代码不包含您的变量和逻辑,此代码用于完美导入和使用方法。
如果这解决了您的问题,请批准此答案。 快乐编码。