【发布时间】:2016-12-06 19:37:41
【问题描述】:
我正在尝试使用包含产品信息的卡片加载网格视图。
我不确定这是否是处理此类数据加载的最佳方式。
首先我要设置我的适配器:
cardView.setAdapter(new productCardAdapter(getActivity(),products));
Products 是一个包含产品对象的对象,产品对象内部具有图像、名称、价格等值。
在我的public class productCardAdapter extends BaseAdapter
我设置产品
this.products = products;
然后在
public View getView(int i, View view, ViewGroup viewGroup) {
我正在根据索引 i 设置值
if(view == null){
// CREATE NEW
view = inflater.inflate(R.layout.product_card,null);
// GET CURRENT PRODUCT
Products.Product product = products.products.get(i);
// CREATE WIDGETS
TextView cardTitle = (TextView)view.findViewById(R.id.txtProductName);
ImageView cardImage = (ImageView)view.findViewById(R.id.imgProductImage);
CardView card = (CardView)view.findViewById(R.id.tmpCard);
TextView cardSKU = (TextView)view.findViewById(R.id.txtProductSKU);
TextView cardPrice = (TextView)view.findViewById(R.id.txtProductPrice);
// GET IMAGE
if (product.picture.length() > 0) {
// PUT IMAGE
cardImage.setImageBitmap(BitmapFactory.decodeFile(functions.getLocalPicturePath(product.picture)));
}else{
// GENERATE GRADIENT
card.setBackground(colors.generateGradient());
}
// SET VALUES
cardTitle.setText(product.name);
cardSKU.setText(product.sku);
cardPrice.setText("$"+product.price);
// CHECK FOR SALE PRICE
if(product.salePrice > 0){
cardPrice.setText("$"+product.salePrice);
}
}
return view;
加载时会出现持续接近 1 秒的断断续续的过渡。
有什么方法可以改进吗?
【问题讨论】:
-
cardImage.setImageBitmap是耗时的。考虑一个延迟加载库