[英]RecyclerView doesn't display all items in list after update android studio
I want to show these items inside my RecyclerView but it doesn't show at all and I can't see the error.我想在我的 RecyclerView 中显示这些项目,但它根本不显示,我看不到错误。 Maybe you guys can help me out.
或许你们可以帮帮我。 My code here:
我的代码在这里:
The Activity:活动:
public class ProductsViewActivity extends AppCompatActivity {
private RecyclerView productView;
private ArrayList<Product> lstProduct;
private RecyclerView.LayoutManager layoutManager;
private ProductAdapter productAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_products_view);
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
lstProduct = new ArrayList<>();
lstProduct.add(new Product(1, "Product 1", "This is good product", "https://www.dollargeneral.com/media/catalog/product/cache/image/700x700/e9c3970ab036de70892d86c6d221abfe/2/0/20706301_kibbles_nbitsbacon_steakflavordrydogfood3.6lb_main.jpg", 34.78));
lstProduct.add(new Product(2, "Product 2", "This is good product", "https://www.iams.com/images/default-source/product-packshot/dog/iams-proactive-health-smart-puppy-large-breed-dog-food.png?sfvrsn=6", 34.78));
productView = findViewById(R.id.productView);
loadProductView();
}
private void loadProductView() {
productView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(this);
productView.setLayoutManager(layoutManager);
productAdapter = new ProductAdapter(lstProduct);
productView.setAdapter(productAdapter);
Log.e("Product view", "Size ======>" + productAdapter.getItemCount());
}
}
The Adapter class:适配器类:
public class ProductAdapter extends RecyclerView.Adapter<ProductAdapter.RecycleViewHolder> {
public static class RecycleViewHolder extends RecyclerView.ViewHolder {
private TextView tvName;
private TextView tvDescription;
private TextView tvPrice;
private TextView tvImage;
public RecycleViewHolder(@NonNull View itemView) {
super(itemView);
tvName = itemView.findViewById(R.id.tvName);
tvDescription = itemView.findViewById(R.id.tvDescription);
tvPrice = itemView.findViewById(R.id.tvPrice);
tvImage = itemView.findViewById(R.id.tvImage);
}
}
private List<Product> lstProduct;
public ProductAdapter(List<Product> lstProduct) {
this.lstProduct = lstProduct;
}
@NonNull
@Override
public RecycleViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
Context context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.product_list_item, parent, false);
RecycleViewHolder viewHolder = new RecycleViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(@NonNull RecycleViewHolder holder, int position) {
holder.tvName.setText(lstProduct.get(position).getName());
holder.tvDescription.setText(lstProduct.get(position).getDescription());
holder.tvPrice.setText(String.valueOf(lstProduct.get(position).getPrice()));
holder.tvImage.setText(String.valueOf(lstProduct.get(position).getImgURL()));
}
@Override
public int getItemCount() {
return lstProduct.size();
}
}
The Product class:产品类别:
public class Product implements Serializable {
private int id;
private String name;
private String description;
private String imgURL;
private double price;
public Product() {
}
public Product(int id, String name, String description, String imgURL, double price) {
this.id = id;
this.name = name;
this.description = description;
this.imgURL = imgURL;
this.price = price;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getImgURL() {
return imgURL;
}
public void setImgURL(String imgURL) {
this.imgURL = imgURL;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
check if "product_list_item" root ViewGroup width is : wrap_content not match_parent.检查“product_list_item”根视图组宽度是否为:wrap_content 不 match_parent。
layou/product_list_item.xml:布局/product_list_item.xml:
<LinearLayout
android:layout_height="wrap_content"
of course you encounter this problem if it was match_parent.当然,如果是match_parent,你会遇到这个问题。
Seems all good, But one thing try to invalidate cache and restart your android studio看起来一切都很好,但是有一件事尝试使缓存无效并重新启动您的 android studio
File > Invalidate caches/Restart文件 > 使缓存无效/重新启动
Hope so, it will work希望如此,它会起作用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.