Android RecyclerView Adapter加载菜单仅显示空白屏问题求助
问题描述
Adapter无法正常展示菜单,仅显示空白屏幕。此前存在代码未初始化的问题,完成初始化操作后代码可正常运行,但原本应展示菜单的页面目前仅显示黑屏。
相关代码
FoodListAdapter代码
public class FoodListAdapter extends RecyclerView.Adapter<FoodListAdapter.MyViewHolder> { private List<Foods> foodList; private FoodListClickListener clickListener; public FoodListAdapter(List<Foods> foodList, FoodListClickListener clickListener) { this.foodList = foodList; this.clickListener = clickListener; } public void updateData(List<Foods> foodList) { this.foodList = foodList; notifyDataSetChanged(); } @NonNull @Override public FoodListAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.menu_recycler_row, parent, false); return new MyViewHolder(view); } @Override public void onBindViewHolder(@NonNull FoodListAdapter.MyViewHolder holder, int position) { holder.menuName.setText(foodList.get(position).getName()); holder.menuPrice.setText("Price: $"+foodList.get(position).getPrice()); holder.addToCartButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Foods food = foodList.get(position); food.setTotalInCart(1); clickListener.onAddToCartClick(food); holder.addMoreLayout.setVisibility(View.VISIBLE); holder.addToCartButton.setVisibility(View.GONE); holder.tvCount.setText(food.getTotalInCart()+""); } }); holder.imageMinus.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Foods food = foodList.get(position); int total = food.getTotalInCart(); total--; if(total > 0 ) { food.setTotalInCart(total); clickListener.onUpdateCartClick(food); holder.tvCount.setText(total +""); } else { holder.addMoreLayout.setVisibility(View.GONE); holder.addToCartButton.setVisibility(View.VISIBLE); food.setTotalInCart(total); clickListener.onRemoveFromCartClick(food); } } }); holder.imageAddOne.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Foods food = foodList.get(position); int total = food.getTotalInCart(); total++; if(total <= 10 ) { food.setTotalInCart(total); clickListener.onUpdateCartClick(food); holder.tvCount.setText(total +""); } } }); Glide.with(holder.thumbImage) .load(foodList.get(position).getUrl()) .into(holder.thumbImage); } @Override public int getItemCount() { List<Foods> foodList =new ArrayList<>(); return foodList.size(); } static class MyViewHolder extends RecyclerView.ViewHolder { TextView menuName; TextView menuPrice; TextView addToCartButton; ImageView thumbImage; ImageView imageMinus; ImageView imageAddOne; TextView tvCount; LinearLayout addMoreLayout; public MyViewHolder(View view) { super(view); menuName = view.findViewById(R.id.menuName1); menuPrice = view.findViewById(R.id.menuPrice1); addToCartButton = view.findViewById(R.id.addToCartButton1); thumbImage = view.findViewById(R.id.thumbImage1); imageMinus = view.findViewById(R.id.imageMinus1); imageAddOne = view.findViewById(R.id.imageAddOne1); tvCount = view.findViewById(R.id.tvCount1); addMoreLayout = view.findViewById(R.id.addMoreLayout1); } } public interface FoodListClickListener { void onAddToCartClick(Foods food); void onUpdateCartClick(Foods food); void onRemoveFromCartClick(Foods food); } }
Food实体类代码
public class Foods implements Parcelable{ private String name; private float price; private String url; private int totalInCart; public int getTotalInCart() { return totalInCart; } public void setTotalInCart(int totalInCart) { this.totalInCart = totalInCart; } protected Foods(Parcel in) { name = in.readString(); price = in.readFloat(); url = in.readString(); totalInCart = in.readInt(); } public static final Creator<Foods> CREATOR = new Creator<Foods>() { @Override public Foods createFromParcel(Parcel in) { return new Foods(in); } @Override public Foods[] newArray(int size) { return new Foods[size]; } }; public String getName() { return name; } public void setName(String name) { this.name = name; } public float getPrice() { return price; } public void setPrice(float price) { this.price = price; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } @Override public int describeContents() { return 0; } @Override public void writeToParcel(Parcel dest, int flags) { dest.writeString(name); dest.writeFloat(price); dest.writeString(url); dest.writeInt(totalInCart); } }
备用菜单JSON数据
"menus":{ "Drinks": [ { "Fresh Fruit Blends": ["Strawberry", "Strawberry Banana","Banana Smoothie","Mango","Papaya","Honeydew","Pineapple", "Avacado", "Cantaloupe","Strawberry Lime","Fruit Tea","Mangonada"], "price": 10.00, "url": "https://firebasestorage.googleapis.com/v0/b/kims-tea-house.appspot.com/o/freshfruit.png?alt=media&token=13915c20-0d40-4542-a74e-20c791b085c2" }, { "100% Natural Juices": ["Orange Juice", "Watermelon", "Carrot", "Carrot Orange","Fresh Limeade","Nuoc Mat(Artichoke Tea"], "price": 10.00, "url": "https://firebasestorage.googleapis.com/v0/b/kims-tea-house.appspot.com/o/nmat100%25naturaljuice.png?alt=media&token=f264240d-1764-480e-8155-b4948b28c36f" }, { "Sea Foam": ["Matcha", "Earl Grey", "Jasmine", "Oolong","Thai Tea","Thai Green", "Black Coffee"], "price": 10.00, "url": "https://firebasestorage.googleapis.com/v0/b/kims-tea-house.appspot.com/o/seafoam.png?alt=media&token=d0175366-3310-4569-bbde-5aa30abcf533" }, { "Cream Tea": ["Matcha", "Cream Tea","Jasmine", "Thai Tea","Thai Green Tea", "Oolong Cream","Brown Sugar Milk","Taro Cream","Almond Cream", "Caramel Cream", "Chocolate Cream", "Coconut Cream", "Honeydew", "Hazelnut", "Oreo Cream Tea", "Peppermint Cream Tea", "Vanilla Cream"], "price": 10.00, "url": "https://firebasestorage.googleapis.com/v0/b/kims-tea-house.appspot.com/o/bsmCreamtea.png?alt=media&token=da1ab8df-4e49-4c02-9080-e1dbb7d5c310" }, { "Flavored Tea": ["Jasmine Green Tea", "Passion Fruit", "Strawberry", "Mango","Peppermint", "Lime","Honey","Dried Plum", "Green Apple","Peach","Lychee","Winter Melon","Pomegranate"], "price": 10.00, "url": "https://firebasestorage.googleapis.com/v0/b/kims-tea-house.appspot.com/o/sbgtea.png?alt=media&token=b74b21cc-8f77-47f1-aeb9-f3ecf744de54" }, { "Smoothie": ["Oreo Cookies & Cream","Taro","Coconut","Pina Colada","Blueberry w/ Ice-Cream","Almond","Red Bean", "Chocolate","Frozen Matcha Green Tea","Orange Dreamsicle","Vanilla"], "price": 10.00, "url": "https://firebasestorage.googleapis.com/v0/b/kims-tea-house.appspot.com/o/tarosmoothie.png?alt=media&token=7fb66ea0-560a-4196-a152-dd09b1229a8c" }, { "Slurpy": ["Kiwi","Blueberry","Cherry","Grape","Green Apple", "Lychee","Passion Fruit","Peach"], "price": 10.00, "url": "https://firebasestorage.googleapis.com/v0/b/kims-tea-house.appspot.com/o/mangoslurpy.png?alt=media&token=07191718-3737-47e1-b2dd-26775fb61fc1" }, {"Coffee": ["Ice-Cream","Frozen Vanilla Cappuccino","Frozen Mocha","Coffee Cream Tea","Cafe Sua Da","Caramel","Hazelnut","Vanilla","Peppermint","Hot Cocoa"], "price": 10.00, "url": "https://firebasestorage.googleapis.com/v0/b/kims-tea-house.appspot.com/o/FVFCOFFEE.png?alt=media&token=3c6f2b6b-3536-490a-838b-1fe027398d54" } ], "Foods" : [ { "name": "Popcorn Chicken", "price": 5.00, "url": "https://firebasestorage.googleapis.com/v0/b/kims-tea-house.appspot.com/o/PopcornCkn.png?alt=media&token=a4112144-2e52-4237-b4a7-781bf0c676dc" }, { "name": "Sweet Potato Fries", "price": 5.00, "url": "https://firebasestorage.googleapis.com/v0/b/kims-tea-house.appspot.com/o/SweetPotato.png?alt=media&token=38e45dfc-e886-4f85-aa3e-d36bc051f6e1" }, { "name": "Egg Rolls", "price": 5.00, "url": "https://firebasestorage.googleapis.com/v0/b/kims-tea-house.appspot.com/o/Eggrolls.png?alt=media&token=221167d6-13b1-48e0-8527-12ee686ba452" }, { "name": "Calamari", "price": 5.00, "url": "https://firebasestorage.googleapis.com/v0/b/kims-tea-house.appspot.com/o/Calamari.png?alt=media&token=004a3bbb-48e9-4392-88ae-2760310ebb00" }, { "name": "Fried Squid Balls", "price": 5.00, "url": "https://firebasestorage.googleapis.com/v0/b/kims-tea-house.appspot.com/o/Squid.png?alt=media&token=634a516f-2d7e-4983-8afb-4c0073ae04e1" }, { "name": "Crinkle Cut Fries", "price": 5.00, "url": "https://firebasestorage.googleapis.com/v0/b/kims-tea-house.appspot.com/o/CrinkleFries.png?alt=media&token=b2a20607-1cfd-405d-96e9-ec92a0512576" } ] }
修复方案
核心问题修复
当前getItemCount方法中局部变量覆盖了类成员变量,每次都返回空列表的长度0,导致RecyclerView认为没有数据可渲染,是黑屏的直接原因。直接修改getItemCount方法即可:
@Override public int getItemCount() { // 返回成员变量foodList的长度,不要新建空列表 return foodList == null ? 0 : foodList.size(); }
其他需排查的问题
- 检查Drinks分类的JSON解析逻辑:Drinks下的菜单项没有
name字段,而是把分类名作为键,解析时需要把分类名赋值给Food类的name属性,否则Drinks列表的菜品名称会为空。 - 检查RecyclerView是否正确设置了LayoutManager,未设置LayoutManager的情况下RecyclerView不会渲染任何内容。
- 检查item布局
menu_recycler_row的根布局高度不要设置为match_parent,否则单个item会占满全屏,看起来和空白效果一致。 - 初始化Adapter或调用
updateData前,确认传入的foodList已经完成JSON解析,不为空。
内容的提问来源于stack exchange,提问作者rebel2theworld
相关产品推荐
相关产品推荐

