gpt4 book ai didi

java - 在 onCreateViewHolder 中使用不同的 View

转载 作者:太空宇宙 更新时间:2023-11-03 13:43:08 25 4
gpt4 key购买 nike

我关注了this Google Developer 的 YouTube channel 的教程,用于实现 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);
}
}

这是没有答案的模拟问题:


编辑:

这是我要求的完整适配器:

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);
}
}
}

此代码不包含您的变量和逻辑,此代码用于完美导入和使用方法。

如果这解决了您的问题,请批准此答案。快乐编码。

关于java - 在 onCreateViewHolder 中使用不同的 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47506739/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com