gpt4 book ai didi

android - 对于包含自己的 recyclerView 的不同 xml 布局,我可以拥有多个 RecyclerViewAdapter 类吗?

转载 作者:行者123 更新时间:2023-12-04 10:02:09 24 4
gpt4 key购买 nike

我已经有一个 RecyclerViewAdapter 类,现在我想要打开另一个 Activity ,点击以前的 recyclerView 的项目,它有自己的 recyclerView。但是当我创建另一个 RecyclerViewAdapter 和 ViewHolder 时,它向我显示了 onCreateViewHolder 方法与 First Adapter 类的方法冲突的错误。我怎么可能有多个 recyclerViewAdapter ......我希望我能解释我想要的。任何帮助将不胜感激。

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder> {

private Context mContext;
private List<Main> mData;

public RecyclerViewAdapter(Context mContext, List<Main> mData) {
this.mContext = mContext;
this.mData = mData;
}

@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view;
LayoutInflater mInflator = LayoutInflater.from(mContext);
view = mInflator.inflate(R.layout.single_view, parent, false);
return new MyViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, final int position) {

holder.textView.setText(mData.get(position).getTitle());
holder.imageView.setImageResource(mData.get(position).getThumbnail());
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

switch (mData.get(position).getTitle()){
case "Grocery & Staples":
//Toast.makeText(mContext, "1", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(mContext, GroceryStaples.class);
mContext.startActivity(intent);
break;
case "Personal Care":
Toast.makeText(mContext, "2", Toast.LENGTH_SHORT).show();
break;
case "Household Items":
Toast.makeText(mContext, "3", Toast.LENGTH_SHORT).show();
break;
case "Beverages":
Toast.makeText(mContext, "4", Toast.LENGTH_SHORT).show();
break;
case "Breakfast & Dairy":
Toast.makeText(mContext, "5", Toast.LENGTH_SHORT).show();
break;
case "Instant Food":
Toast.makeText(mContext, "6", Toast.LENGTH_SHORT).show();
break;
case "Fruits & Vegetables":
Toast.makeText(mContext, "7", Toast.LENGTH_SHORT).show();
break;
default:
Toast.makeText(mContext, "NO", Toast.LENGTH_SHORT).show();
break;
}
}
});

}

@Override
public int getItemCount() {
return mData.size();
}

@Override
public int getItemViewType(int position) {
return super.getItemViewType(position);
}

public class MyViewHolder extends RecyclerView.ViewHolder {

//Main Activity
ImageView imageView;
TextView textView;

public MyViewHolder(@NonNull View itemView) {
super(itemView);
mContext = itemView.getContext();
//Main Activity
imageView = itemView.findViewById(R.id.imageView);
textView = itemView.findViewById(R.id.textView);


}

}

}

新适配器:

public class RvAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {

private Context mContext;
private List<Main> mData;

public RvAdapter(Context mContext, List<Main> mData){
this.mContext = mContext;
this.mData = mData;
}

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view;
LayoutInflater mInflator = LayoutInflater.from(mContext);
view = mInflator.inflate(R.layout.rv_item_click, parent, false);
return null
}

@Override
public void onBindViewHolder(@NonNull RecyclerViewAdapter.MyViewHolder holder, int position) {

}


@Override
public int getItemCount() {
return 0;
}

public class ViewHolder extends RecyclerView.ViewHolder{

public ViewHolder(@NonNull View itemView) {
super(itemView);
}
}
}

最佳答案

在您的 NewAdapter.java代替:

public class RvAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder>


public class RvAdapter extends RecyclerView.Adapter<RvAdapter.ViewHolder>

并替换:
public void onBindViewHolder(@NonNull RecyclerViewAdapter.MyViewHolder holder, int position)


public void onBindViewHolder(@NonNull RvAdapter.ViewHolder holder, int position)

关于android - 对于包含自己的 recyclerView 的不同 xml 布局,我可以拥有多个 RecyclerViewAdapter 类吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61771365/

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