GridView使用示例(一)

简介: main.xml如下: gridview_item.xml如下:   MainActivity如下: import java.

main.xml如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

    <GridView
        android:id="@+id/gridView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:stretchMode="columnWidth"
        android:verticalSpacing="5dip"
        android:horizontalSpacing="5dip"
        android:numColumns="3"
        />

</RelativeLayout>


gridview_item.xml如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >
    <ImageView 
        android:id="@+id/imageView"
        android:layout_height="100dip"
        android:layout_width="150dip"
        android:scaleType="fitXY"
       />
    <TextView 
        android:id="@+id/textView"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_gravity="center_horizontal"
      />

</LinearLayout>


 

MainActivity如下:

import java.util.ArrayList;
import java.util.HashMap;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import android.app.Activity;
import android.content.res.TypedArray;
/**
 *Demo描述:利用GridView展示图片
 *注意:
 *图片的id是存放在array.xml中的,注意TypedArray的使用
 */
public class MainActivity extends Activity {
    private GridView mGridView;
    TypedArray photoIDArray=null;
    private String [] photoNameArray=null;
    private ArrayList<HashMap<String, Object>> gridViewItemsArrayList;
    private HashMap<String, Object> everyItemHashMap;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		init();
	}
    private void init(){
    	mGridView=(GridView) findViewById(R.id.gridView);
    	photoIDArray=getResources().obtainTypedArray(R.array.photoIDArray);
    	photoNameArray=getResources().getStringArray(R.array.photoNameArray);
    	gridViewItemsArrayList=new ArrayList<HashMap<String,Object>>();
    	for (int i = 0; i < photoNameArray.length; i++) {
			everyItemHashMap=new HashMap<String, Object>();
			everyItemHashMap.put("photoID",  (int)photoIDArray.getResourceId(i,0));
			everyItemHashMap.put("photoName", photoNameArray[i]);
			gridViewItemsArrayList.add(everyItemHashMap);
		}
    	SimpleAdapter simpleAdapter=new SimpleAdapter(MainActivity.this, gridViewItemsArrayList,
    	R.layout.gridview_item, new String []{"photoID","photoName"}, new int[]{R.id.imageView,R.id.textView});
        mGridView.setAdapter(simpleAdapter);
        mGridView.setOnItemClickListener(new ItemClickListenerImpl());
    }
    private class ItemClickListenerImpl implements OnItemClickListener{
		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
			TextView textView=(TextView) view.findViewById(R.id.textView);
			String name=textView.getText().toString();
			Toast.makeText(MainActivity.this, position+" "+name, Toast.LENGTH_SHORT).show();
		}
    	
    }
}


 

相关文章
|
7月前
|
XML 数据格式
ListView示例(附详解+源码)
ListView示例(附详解+源码)
77 3
|
.NET 开发框架 Go
GridView控件自定义分页的实现
前人栽树,后人乘凉,话不多说,代码如下:     实现方式一: .aspx: [c-sharp] view plain copy <form id="form1" runat="server">       <table style="width: 605px">         .
1434 0
列表ListBox、ListView、GridView 排序
列表排序 1.使用控件默认排序方式(推荐) ListControl.Items.SortDescriptions.Clear(); ListControl.Items.SortDescriptions.
895 0