android之GridView不同实现方式详解

1.利用SimpleAdapter适配器实现。

这里以每一个网格中添加一张图片和相应的文字说明为例:

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <GridView android:id="@+id/gridView"
        android:numColumns="4"
        android:horizontalSpacing="10px"
        android:verticalSpacing="10px"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        />
</LinearLayout>


因为除了添加图片之外,还相应的添加说明性的文字,所以这里需要为每网格设置一个布局item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:gravity="center_horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <ImageView android:id="@+id/imageView"
        android:layout_weight="4"
        android:scaleType="fitCenter"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
    <TextView android:id="@+id/textView"
        android:layout_weight="1"
        android:gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="24px"
        />
</LinearLayout>

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<String, Object>>list = new ArrayList<Map<String, Object>>();
		for(int i = 0; i < imageId.length; i++)
		{
			Map<String, Object> map = new HashMap<String, Object>();
			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实现添加图片:

这里我们分两种类型进行讲解:

@1:如果只是涉及添加图像,没有相应的文字说明,这样的话BaseAdapter的实现方式就比较简单。

这里

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <GridView android:id="@+id/gridView"
        android:numColumns="4"
        android:horizontalSpacing="10px"
        android:verticalSpacing="10px"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        />
</LinearLayout>

此时不再需要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;
			}



android之GridView不同实现方式详解,,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。