android之GridView不同實現方式詳解

1.利用SimpleAdapter適配器實現。

這裡以每一個網格中添加一張圖片和相應的文字說明為例:

main.xml


    

因為除瞭添加圖片之外,還相應的添加說明性的文字,所以這裡需要為每網格設置一個佈局item.xml


    
    

MainActivity.java中的代碼:

public class MainActivity extends Activity {
	
	private int []imageId = new int[]{R.drawable.img01, R.drawable.img02, R.drawable.img03, R.drawable.img04, 
			R.drawable.img05, R.drawable.img06, R.drawable.img07, R.drawable.img08, R.drawable.img09, 
			R.drawable.img10, R.drawable.img11, R.drawable.img12};
	private String[] title = new String[]{"花開富貴", "海天一色", "日出", "天路", "一枝獨秀", "雲", "獨占鰲頭", 
			"蒲公英花", "花團錦簇", "爭奇鬥艷", "和諧", "林間小路"};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main); 
		
		
		GridView gridView = (GridView)findViewById(R.id.gridView);
		List<Map>list = new ArrayList<Map>();
		for(int i = 0; i < imageId.length; i++)
		{
			Map map = new HashMap();
			map.put("image", imageId[i]);
			map.put("title", title[i]);
			list.add(map);
		}
		SimpleAdapter adapter = new SimpleAdapter(MainActivity.this, list, R.layout.item, new String[]{"image", "title"}, new int[]{R.id.imageView, R.id.textView});
		gridView.setAdapter(adapter);
	}

2.利用BaseAdapter實現添加圖片:vc3Ryb25nPjwvcD4KPHA+1eLA787Sw8e31sG91tbA4NDNvfjQ0L2yveKjujwvcD4KPHA+QDGjusjnufvWu8rHyea8sMztvNPNvM/xo6zDu9PQz+DTprXEzsTX1su1w/ejrNXi0fm1xLuwQmFzZUFkYXB0ZXK1xMq1z9a3vcq9vs2xyL3PvPK1paGjPC9wPgo8cD7V4sDvPC9wPgo8cD5tYWluLnhtbDwvcD4KPHA+PC9wPgo8cHJlIGNsYXNzPQ==”brush:java;”>

此時不再需要item.xml.

MainActivity.java中的代碼:

BaseAdapter需要重寫getView()(最重要的)、getItem()、getItemId()、getCount()這四個方法。

還有就是convertView是一個緩存View。

通過緩存convertView,這種利用緩存contentView的方式可以判斷如果緩存中不存在View才創建View,如果已經存在可以利用緩存中的View,提升瞭性能

public class MainActivity extends Activity {
	
	private int []imageId = new int[]{R.drawable.img01, R.drawable.img02, R.drawable.img03, R.drawable.img04, 
			R.drawable.img05, R.drawable.img06, R.drawable.img07, R.drawable.img08, R.drawable.img09, 
			R.drawable.img10, R.drawable.img11, R.drawable.img12, };
	//private String[] title = new String[]{"花開富貴", "海天一色", "日出", "天路", "一枝獨秀", "雲", "獨占鰲頭", 
			//"蒲公英花", "花團錦簇", "爭奇鬥艷", "和諧", "林間小路"};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main); 
		
		GridView gridView = (GridView)findViewById(R.id.gridView);
		BaseAdapter adapter = new BaseAdapter()
		{
			@Override
			public int getCount() {
				// TODO 自動生成的方法存根
				return imageId.length;
			}
			@Override
			public Object getItem(int position) {
				// TODO 自動生成的方法存根
				return position;
			}
			@Override
			public long getItemId(int position) {
				// TODO 自動生成的方法存根
				return position;
			}
			@Override
			public View getView(int position, View convertView, ViewGroup parent) {
				// TODO 自動生成的方法存根
				ImageView imageView;
				if(convertView == null)
				{
					imageView = new ImageView(MainActivity.this);
					imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
					imageView.setLayoutParams(new GridView.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
				}
				else
					imageView = (ImageView)convertView;
				imageView.setImageResource(imageId[position]);
				return imageView;
			}
		};
		gridView.setAdapter(adapter);
		
	}

3.利用BaseAdapter實現添加圖片與文字說明。

這裡不僅要添加圖片還要添加相應的文字說明,相對上面的方法2比較復雜。但這確是BaseAdpter的最經典的用法。這裡涉及到效率問題,因而又有三種實現方式。

1.實現最簡單,但效率最低。

這種方式沒有采用convertView緩存機制。

其中LayoutInflater作用是將佈局文件實例化為View。

main.xml與item.xml不變。

MainActivity.java中的代碼如下:

public class MainActivity extends Activity {
	
	private int []imageId = new int[]{R.drawable.img01, R.drawable.img02, R.drawable.img03, R.drawable.img04, 
			R.drawable.img05, R.drawable.img06, R.drawable.img07, R.drawable.img08, R.drawable.img09, 
			R.drawable.img10, R.drawable.img11, R.drawable.img12, };
	private String[] title = new String[]{"花開富貴", "海天一色", "日出", "天路", "一枝獨秀", "雲", "獨占鰲頭", 
			"蒲公英花", "花團錦簇", "爭奇鬥艷", "和諧", "林間小路"};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main); 
		

		final LayoutInflater mInflater = LayoutInflater.from(this);
		GridView gridView = (GridView)findViewById(R.id.gridView);
		BaseAdapter adapter = new BaseAdapter()
		{
			@Override
			public int getCount() {
				// TODO 自動生成的方法存根
				return imageId.length;
			}
			@Override
			public Object getItem(int position) {
				// TODO 自動生成的方法存根
				return position;
			}
			@Override
			public long getItemId(int position) {
				// TODO 自動生成的方法存根
				return position;
			}
			@Override
			public View getView(int position, View convertView, ViewGroup parent) {
				// TODO 自動生成的方法存根
				View view = mInflater.inflate(R.layout.item, null);
				ImageView imageView = (ImageView)view.findViewById(R.id.imageView);
				TextView textView = (TextView)view.findViewById(R.id.textView);
				imageView.setImageResource(imageId[position]);
				textView.setText(title[position]);
				return view;
			}
			
		};
		gridView.setAdapter(adapter);
		
	}

2.采用緩存,效率提高將近200%

除瞭getView()不同之外,其他和方法1完全相同。

@Override
			public View getView(int position, View convertView, ViewGroup parent) {
				// TODO 自動生成的方法存根
				if(convertView == null)
				{
					convertView = mInflater.inflate(R.layout.item, null);
				}
				ImageView imageView = (ImageView)convertView.findViewById(R.id.imageView);
				TextView textView = (TextView)convertView.findViewById(R.id.textView);
				imageView.setImageResource(imageId[position]);
				textView.setText(title[position]);
				return convertView;
			}
			
		};

3.設置標簽。效率再次提升50%。

			class ViewHolder
			{
				private ImageView imageView;
				private TextView textView;
			}
			@Override
			public View getView(int position, View convertView, ViewGroup parent) {
				// TODO 自動生成的方法存根
				ViewHolder holder;
				if(convertView == null)
				{
					convertView = mInflater.inflate(R.layout.item, null);
					holder = new ViewHolder();
					holder.imageView = (ImageView)convertView.findViewById(R.id.imageView);
					holder.textView = (TextView)convertView.findViewById(R.id.textView);
					convertView.setTag(holder);
				}
				holder = (ViewHolder)convertView.getTag();
				
				holder.imageView.setImageResource(imageId[position]);
				holder.textView.setText(title[position]);
				return convertView;
			}

發佈留言