Gridview加载随机图像

时间:2014-04-03 12:54:14

标签: android gridview baseadapter

我已经实现了视图持有者模式,该模式旨在重用项目。 每次我重新加载应用程序时,它会加载不同的图像,而不是整个列表。 我正在使用一个包含20 R.Drawable.pic_x

的ArrayList
public class SetWallpaperAdapter extends BaseAdapter {

private Context mContext;
private ArrayList<Integer> mResourceIdList;
static int mWidth;

public SetWallpaperAdapter(Context context){
    mContext = context;
    initWallpapersList();
    DisplayMetrics metrics = context.getResources().getDisplayMetrics();
    mWidth = metrics.widthPixels / 2 -10;
}

@Override
public int getCount() {
    return mResourceIdList.size();
}

@Override
public Object getItem(int position) {
    return mResourceIdList.get(position);
}

@Override
public long getItemId(int position) {
    return 0;
}

// our ViewHolder.
// caches our TextView
private static class ViewHolderItem {

    public ImageView mImageViewItem;

    public void initHolder (View view) {
         mImageViewItem = (ImageView) view.findViewById(R.id.wallpaper_image);
    }

    public void updateView (final int resId) {

        // assign values if the object is not null
        if(resId != 0) {
            // get the TextView from the ViewHolder and then set the text (item name) and tag (item ID) values
            Picasso.with(mImageViewItem.getContext()).load(resId).resize(mWidth,(int)(mWidth *1.5)).into(mImageViewItem);
            mImageViewItem.setTag(resId);

        }
    }
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    ViewHolderItem viewHolder;

    if(convertView==null){
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.wallpaper_item, parent, false);
        viewHolder = new ViewHolderItem();
        viewHolder.initHolder(convertView);
        convertView.setTag(viewHolder);
        Log.d("aaa", "NEW!");
    }else{
        viewHolder = (ViewHolderItem) convertView.getTag();
        Log.d("aaa", "RESUSE");
    }

    // object item based on the position
    int resId = mResourceIdList.get(position);

    viewHolder.updateView(resId);

    return convertView;
}

/* Static resources */
private void initWallpapersList() {

    mResourceIdList = new ArrayList<Integer>();

    mResourceIdList.add(R.drawable.angry_lion);
    mResourceIdList.add(R.drawable.apple_rainbow_fly);
    mResourceIdList.add(R.drawable.apple_returns);
    mResourceIdList.add(R.drawable.aqua_blue);
    mResourceIdList.add(R.drawable.baloon_blur);
    mResourceIdList.add(R.drawable.blur_times);
    mResourceIdList.add(R.drawable.catwoman);
    mResourceIdList.add(R.drawable.clown);
    mResourceIdList.add(R.drawable.cold_kasima);
    mResourceIdList.add(R.drawable.daft_punk_two);
    mResourceIdList.add(R.drawable.geometric);
    mResourceIdList.add(R.drawable.ios7);
    mResourceIdList.add(R.drawable.minion_hitman);
    mResourceIdList.add(R.drawable.night_stars_over_mt);
    mResourceIdList.add(R.drawable.primary_color);
    mResourceIdList.add(R.drawable.prosper_mountain);
    mResourceIdList.add(R.drawable.rolling_stones);
    mResourceIdList.add(R.drawable.shimmer_art_blue);

}
}

我的问题是,每次打开应用程序时,不同的图像会加载到Gridview中,而不是所有图像。应该是在initWallpaperList()中初始化的所有ArrayList都应该显示在gridview中。此外,该列表包含18个项目,当列表最初加载时,getView()被调用26次。

0 个答案:

没有答案