GridView 中getview方法position 0 加载多次问题

这的确是一个让人头疼的问题,关键点在于gridview布局文件是自适应的,如果是定长就没有这个问题了,可是现在正常情况都需要屏幕翻转,这种情况也许翻转时重新设置高度就行了,但是如果是对应不同的机器那就不行了,尺寸不一样。
所以我在上一节代码中已经做了优化,既然在我的ArrayList中已经存放了position为0时的bitmap,那直接获取就行了,不需要再继续往下走。

[代码]java代码:

/**
 *
 * Copyright 2012 HISENSE. All rights reserved.
 * DirListAdapter.java
 *
 */
package com.hisense.hicloud.album.hiview;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

import com.hisense.hicloud.album.HiCloudAlbum;
import com.hisense.hicloud.album.HiCloudApplication;
import com.hisense.hicloud.album.R;
import com.hisense.hicloud.album.imagecache.AsyncImageLoader;
import com.hisense.hicloud.album.imagecache.HiImageView;
import com.hisense.hicloud.album.imagecache.LocationImageCache;
import com.hisense.hicloud.album.imagecache.AsyncImageLoader.BitmapCallback;
import com.hisense.hicloud.album.imagecache.LocationImageCache.OnLoadImageListener;
import com.hisense.hicloud.album.scanfiles.ImageListBean;
import com.hisense.hicloud.album.sys.SYSPath;

/**
 * @author qiuxiaolong ([email protected])
 * @date 2012-2-13
 */
public class LocalImageAdapter2 extends BaseAdapter {

	private Context context;

	private static ArrayList imagePathList;

	private LayoutInflater inflater;

	private ViewHolder holder = null;

	private AsyncImageLoader imageLoader; 
	private String LocalThumbPath;//本地图片缩略图地址
	private static String TAG = "LocalImageAdapter2";
    /**存放bitmap与position*/
    public static ArrayList> bitmapArrayList = new ArrayList>();
    //获取LocalGridView对象
	private GridView gridview;
	/**记步,记录getView执行次数,六次一循环*/
	private static int local_i = 0;
	/**记录gridview中第一张和最后一张图片的position*/
	private static int gridviewFirst = 0;
	private static int gridviewLast = 0;
	public LocalImageAdapter2(){}
	public LocalImageAdapter2(Context context,ArrayList callList,GridView gridview) {
		this.context = context;
		this.imagePathList = callList;
		for (String url : imagePathList) {
			Log.d("==========Image Url==========", url);
		}

		inflater = LayoutInflater.from(this.context);
		imageLoader = new AsyncImageLoader(true, context);
		this.gridview = gridview;
		//judge that whether the bitmapArrayList  is null
		CloudImageAdapter cloudimageadapter = new CloudImageAdapter();
		if(cloudimageadapter.getbitmapArrayListSize() != 0){
			//clean the bitmapArrayList
			cloudimageadapter.cleanbitmapArrayList();
		}
	}

	public void removeAdpterList(ArrayList removeList)
	{
	    System.out.println("=============删除元数据:"+removeList.size());
	    System.out.println("=============前:"+imagePathList.size());
	    imagePathList.removeAll(removeList);
	    System.out.println("=============后:"+imagePathList.size());
	}
	
	public int getCount() {
		return imagePathList.size();
	}

	public Object getItem(int position) {
		return imagePathList.get(position);
	}

	public long getItemId(int position) {
		return position;
	}
	/**return bitmapArrayList size*/
	public int getbitmapArrayListSize(){
		return bitmapArrayList.size();
	}
	/**clean bitmapArrayList all*/
	public void cleanbitmapArrayList(){
		for(int k = 0;k < bitmapArrayList.size();k++){
			if(bitmapArrayList.get(k).get("bitmap") != null){
				//get bitmap from hashmap
				Bitmap bitmap1 = (Bitmap) bitmapArrayList.get(k).get("bitmap");
				try{
					if(!bitmap1.isRecycled()){
						bitmap1.recycle();
						Log.i(TAG,"clean bitmapArrayList success,print position:" + (Integer)bitmapArrayList.get(k).get("ID"));
						//remove this element
						bitmapArrayList.remove(k);
						k--;
					}
				}catch(Exception e){
					Log.e(TAG,"clean bitmapArrayList error");
					e.printStackTrace();
				}
			}
		}
	}
	/**clean bitmap that not in gridview*/
	public void cleanGridView(){
		//select bitmaps what can be recycled
		for(int k = 0;k < bitmapArrayList.size();k++){
			//keep these bitmaps which between first and last
			if((Integer)bitmapArrayList.get(k).get("ID")>=(gridviewFirst-3) && (Integer)bitmapArrayList.get(k).get("ID") <= (gridviewLast + 3)){
				//in the range,do nothing
				Log.i(TAG,"in the range continue,print the k:" + k + ";and print the position:" + (Integer)bitmapArrayList.get(k).get("ID"));
				continue;
			}
			//get the bitmap from hashmap
			Bitmap bitmap1 = (Bitmap) bitmapArrayList.get(k).get("bitmap");
			if(bitmap1 != null){
				try{
					Log.i(TAG,"start to recycle,print the position:" + (Integer)bitmapArrayList.get(k).get("ID") + ";print first position:" 
							+ gridviewFirst + ";print last position:" + gridviewLast);
					if(!bitmap1.isRecycled()){
						Log.i(TAG,"can be recycled,the id of bitmap:" + bitmap1.toString());
						//do the action
						bitmap1.recycle();
						Log.i(TAG,"success to recycle,print the position:" + (Integer)bitmapArrayList.get(k).get("ID"));
						//remove this element
						bitmapArrayList.remove(k);
						k--;
					}
				}catch(RuntimeException e){
					Log.e(TAG,"the bitmap can't be recycled,print the position:" + (Integer)bitmapArrayList.get(k).get("ID"));
					e.printStackTrace();
				}
			}
		}
		//do gc
		System.gc();
	}
	public View getView( final int position, View convertView, final ViewGroup parent) {
		Log.e(TAG,"print the position:" + position + ";print bitmapArrayList size:" + bitmapArrayList.size());
		//get current first position and last position 
		gridviewFirst = gridview.getFirstVisiblePosition();
		gridviewLast = gridview.getLastVisiblePosition();
		//do the clean action for every six
		if(local_i == 6){
			local_i = 0;
			//do the clean action
			//cleanGridView();
		}
		
		if (convertView == null) {
			holder = new ViewHolder();
			if(HiCloudAlbum.album_orientation == HiCloudAlbum.ALBUM_ORIENTATION_LANDSCAPE)
				convertView = inflater.inflate(R.layout.image_local_gridview_item_land, null);
			else
					convertView = inflater.inflate(R.layout.image_local_gridview_item, null);
			holder.img = (HiImageView) convertView
					.findViewById(R.id.image_thumbnail);
			holder.mask = (ImageView) convertView.findViewById(R.id.masklayer);
			holder.uploaded = (ImageView)convertView.findViewById(R.id.uploadedlayer);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		holder.img.setTag(imagePathList.get(position));
		
		if (LocalGridView.getAlreadUploadList().contains(imagePathList.get(position)))
		{
			holder.uploaded.setVisibility(View.VISIBLE);
			holder.mask.setVisibility(View.GONE);
		}else
		{
			if (LocalGridView.getSelectedItemsList().contains(imagePathList.get(position)))
			{
			    holder.mask.setVisibility(View.VISIBLE);
			}else
			{
			    holder.mask.setVisibility(View.GONE);
			}
			holder.uploaded.setVisibility(View.GONE);
		}
		//计算本地缩略图存放地址
		LocalThumbPath = SYSPath.LocalThumCachePATH + "/" + imagePathList.get(position).substring(imagePathList.get(position).lastIndexOf("/")+1) + "_Thum.jpg";
		Bitmap bitmap = null;
		boolean flag = false;
		Log.d(TAG,"输出LocalThumbPath" + LocalThumbPath);
		//if the bitmap is in the bitmapArrayList,then get it from bitmapArrayList 
		/*for(int i = 0;i map = new HashMap();
		map.put("ID", position);
		//map.put("bitmap", bitmap);
		//add bitmapArrayList
		bitmapArrayList.add(map);
	    Log.d(TAG,"success and over at section " + section);
    }
	public class ViewHolder {
		public HiImageView img;
		public ImageView mask;
		public ImageView uploaded;
	}
}
有一点需要注意,我用的回调,所以geview执行速度很快,此时会有position 0还是会重复出现的情况,那么就必须在回调过程加载bitmap之前再次执行搜索语句,判断数组中是否存在position0,因为我的bitmap加载都是在消息队列中,所以这个肯定是一步一步执行的,不会出现我说的情况。

你可能感兴趣的:(android)