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 (qiuxiaolong1@hisense.com)
 * @date 2012-2-13
 */
public class LocalImageAdapter2 extends BaseAdapter {

	private Context context;

	private static ArrayList<String> imagePathList;

	private LayoutInflater inflater;

	private ViewHolder holder = null;

	private AsyncImageLoader imageLoader; 
	private String LocalThumbPath;//本地图片缩略图地址
	private static String TAG = "LocalImageAdapter2";
    /**存放bitmap与position*/
    public static ArrayList<HashMap<String,Object>> bitmapArrayList = new ArrayList<HashMap<String,Object>>();
    //获取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<String> 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<String> 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<bitmapArrayList.size();i++){
			//Comparison the position
			if(bitmapArrayList.get(i).get("ID").toString().equals(String.valueOf(position))){
				Log.i(TAG,"have the same position,print this position:" + position);
				//get bitmap from hashmap
				bitmap = (Bitmap)bitmapArrayList.get(i).get("bitmap");
				holder.img.setImageBitmap(null);
			    holder.img.setImageBitmap(bitmap);
			    //set to be true,then skip getting bitmap from local
				flag = true;
				break;
			}
		}*/
		if(flag == false){
			//mark add
			local_i++;
			Log.i(TAG,"print the local_i" + local_i);
			//get bitmap from local
			//bitmap = AsyncImageLoader.getCacheBitmap(LocalThumbPath,position);
			bitmap = AsyncImageLoader.getCacheBitmap(imagePathList.get(position));
			if (bitmap != null)
			{
				Log.i(TAG,"success to get the bitmap at section one");
				holder.img.setImageBitmap(null);
			    holder.img.setImageBitmap(bitmap);
			    //bitmapArrayList add
	            bitmapArrayListAdd(position,bitmap,"one");
			}else{
				Log.i(TAG,"can't find bitmap from local,bitmap is null,now start to create thumb picture and get bitmap from it");
				//bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.fipper_loading);
			    holder.img.setImageBitmap(null);
			    imageLoader.loadLocalBitmap(imagePathList.get(position),LocalThumbPath,new BitmapCallback() {
			    //imageLoader.loadLocalBitmap(imagePathList.get(position),LocalThumbPath, position,new BitmapCallback() {
			    public void imageLoaded(Bitmap imageBitmap, String imageUrl)
	    	            {
	    	                // TODO Auto-generated method stub
	    	                HiImageView iv = (HiImageView) parent.findViewWithTag(imageUrl);
	    	                if (iv != null && imageBitmap != null)
	    	                {
	    	    				Log.i(TAG,"success to get the bitmap at section two");
	    	                	iv.setImageBitmap(null);
	    	                    iv.setImageBitmap(imageBitmap);
	    	                  //bitmapArrayList add
	    	    	            bitmapArrayListAdd(position,imageBitmap,"two");
	    	                }
	    	            }
	    	        });
			}
		}
		return convertView;

	}

    /**bitmapArrayList add*/
    private void bitmapArrayListAdd(int position,Bitmap bitmap,String section){
    	Log.d(TAG,"at section "+section+" print the id of bitmap:" + bitmap.toString() + ";print the position" + position);
	    //create map to put information
		HashMap<String,Object> map = new HashMap<String,Object>();
		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加载都是在消息队列中,所以这个肯定是一步一步执行的,不会出现我说的情况。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值