我正在尝试使用适配器包含适配器 recyleview 错误显示来修复此错误。在此 AdapterGrid 下方包含 AdapterRecipes 添加适配器错误显示。我尝试在下面添加此错误显示添加图像如何修复错误错误显示->此片段添加适配器网格错误显示如何清除错误
public class AdapterGrid extends RecyclerView.Adapter<AdapterGrid.ViewHolder> {
private List<Category> items;
private Context context;
private OnItemClickListener mOnItemClickListener;
SharedPref sharedPref;
public FragmentActivity activity;
public AdapterGrid(FragmentActivity activity, List<Category> items) {
this.items = items;
this.context = context;
this.sharedPref = new SharedPref(context);
}
public interface OnItemClickListener {
void onItemClick(View view, Category obj, int position);
}
public void setOnItemClickListener(final OnItemClickListener mItemClickListener) {
this.mOnItemClickListener = mItemClickListener;
}
// Provide a suitable constructor (depends on the kind of dataset)
public AdapterGrid(Context context, Activity activity, ArrayList<Category> items) {
this.items = items;
this.context = context;
this.sharedPref = new SharedPref(context);
}
public class ViewHolder extends RecyclerView.ViewHolder {
// each data item is just a string in this case
public TextView subcategory_text;
public TextView recipe_count;
public ImageView category_image;
public RecyclerView horizontal_rv;
public MaterialRippleLayout lyt_parent;
public ViewHolder(View v) {
super(v);
subcategory_text = v.findViewById(R.id.subcategory_text);
// recipe_count = v.findViewById(R.id.video_count);
category_image = v.findViewById(R.id.category_image);
lyt_parent = v.findViewById(R.id.lyt_parent);
horizontal_rv = itemView.findViewById(R.id.horizontal_rv);
}
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.vertical_item, parent, false);
return new ViewHolder(v);
}
// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(ViewHolder holder, final int position) {
final Category c = items.get(position);
holder.subcategory_text.setText(c.category_name);
holder.horizontal_rv.setLayoutManager(new LinearLayoutManager(activity, LinearLayoutManager.HORIZONTAL, false));
AdapterRecipes adapter = new AdapterRecipes(context, activity, (Call<CallbackCategories>) items, R.layout.vertical_item);
holder.horizontal_rv.setAdapter(adapter);
// if (AppConfig.ENABLE_RECIPE_COUNT_ON_CATEGORY) {
//// holder.recipe_count.setVisibility(View.VISIBLE);
// holder.recipe_count.setText(c.recipes_count + " " + context.getResources().getString(R.string.recipes_count_text));
// } else {
// holder.recipe_count.setVisibility(View.GONE);
// }
Picasso.get()
.load(sharedPref.getApiUrl() + "/upload/category/" + c.category_image)
.placeholder(R.drawable.ic_thumbnail)
.into(holder.category_image);
holder.lyt_parent.setOnClickListener(view -> {
if (mOnItemClickListener != null) {
mOnItemClickListener.onItemClick(view, c, position);
}
});
}
public void setListData(List<Category> items){
this.items = items;
notifyDataSetChanged();
}
public void resetListData() {
this.items = new ArrayList<>();
notifyDataSetChanged();
}
// Return the size of your dataset (invoked by the layout manager)
@Override
public int getItemCount() {
return items.size();
}
}
此片段添加adapterGrid错误显示如何清除错误
private void displayCategory(List<Category> items) {
RecyclerView recyclerView = root_view.findViewById(R.id.recycler_view_category);
recyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
AdapterGrid AdapterGrid = new AdapterGrid(getContext(), (Activity) getActivity(), (ArrayList<Category>) items);
recyclerView.setAdapter(AdapterGrid);
recyclerView.setNestedScrollingEnabled(false);
AdapterGrid.setOnItemClickListener((view, obj, position) -> {
Intent intent = new Intent(getActivity(), ActivityCategoryDetail.class);
intent.putExtra(EXTRA_OBJC, obj);
startActivity(intent);
((MainActivity) Objects.requireNonNull(getActivity())).showInterstitialAd();
});
LinearLayout lyt_category = root_view.findViewById(R.id.lyt_category);
if (items.size() > 0) {
lyt_category.setVisibility(View.VISIBLE);
} else {
lyt_category.setVisibility(View.GONE);
}
}
有
items
的铸件,具有 List<Category>
类型到 Call<CallbackCategories>
类型。看起来这是一个问题 - 请仔细检查该转换是否可行。