2016-12-06 95 views
0

我想加载一个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秒的断断续续过渡时

的值。

有什么方法可以改进?

+1

那'cardImage.setImageBitmap'是耗时的。考虑一个懒加载库 – njzk2

回答

0

谷歌的示例代码https://developer.android.com/guide/topics/ui/layout/gridview.html正在设置一个不好的例子

为了解决这个问题的示例代码,而不是:

imageView.setImageResource(mThumbIds[position]); 

你要使用一个库,我用滑翔,在一个线程加载图像。就像这样:

public View getView(int position, View convertView, ViewGroup parent) { 
    CircleImageView imageView; 

    if (convertView == null) { 
     // not recycled 
     View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_image, parent, false); 
     imageView = (CircleImageView) view; 
    } else { 
     // recycled 
     imageView = (CircleImageView) convertView; 
    } 

    Glide 
      .with(parent.getContext()) 
      .load(mThumbIds[position]) 
      .placeholder(R.mipmap.ic_launcher)//REQUIRED 
      .dontAnimate()//REQUIRED 
      .into(imageView); 

    //imageView.setImageResource(mThumbIds[position]); 

    return imageView; 
} 

这里获取CircleImageView:https://github.com/hdodenhof/CircleImageView 获取滑翔这里:https://github.com/bumptech/glide

顺便说一句,我R.layout.item_image的XML是:

<?xml version="1.0" encoding="utf-8"?> 

<de.hdodenhof.circleimageview.CircleImageView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/image_avatar" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:scaleType="centerCrop" 
    app:civ_border_width="1dp" 
    app:civ_border_color="#FF000000"/>