我想加载一个gridview与持有产品信息卡。加载Gridview是不稳定
我不确定这是否是处理这种数据加载的最佳方式。
首先我设置我的适配器:
cardView.setAdapter(new productCardAdapter(getActivity(),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'是耗时的。考虑一个懒加载库 – njzk2