标签:
MainActivity代码
1 package gridviewdemo4.example.administrator.gridviewdemo04; 2 3 import android.app.Activity; 4 import android.support.v7.app.AppCompatActivity; 5 import android.os.Bundle; 6 import android.view.View; 7 import android.widget.AdapterView; 8 import android.widget.GridView; 9 import android.widget.SimpleAdapter; 10 import android.widget.Toast; 11 12 import java.util.ArrayList; 13 import java.util.HashMap; 14 import java.util.List; 15 import java.util.Map; 16 17 public class MainActivity extends Activity implements AdapterView.OnItemClickListener{ 18 GridView gridView; 19 private List<Map<String,Object>> datalist; 20 21 private SimpleAdapter simpleAdapter; 22 /* 1设置数据源 23 2 新建适配器 24 3 GridView加载到适配器 25 4 GridView配置事件监听器(实现OnItemClickListener接口)*/ 26 27 private int[] icon={R.drawable.calendar, R.drawable.camera,R.drawable.clock,R.drawable.games_control,R.drawable.messenger,R.drawable.ringtone 28 ,R.drawable.settings,R.drawable.speech_balloon,R.drawable.weather,R.drawable.world,R.drawable.youtube}; 29 private String[] iconName={"日历","照相机","闹钟","游戏","联系人","音乐","设置","短信息","天气","浏览器","优土"}; 30 @Override 31 protected void onCreate(Bundle savedInstanceState) { 32 super.onCreate(savedInstanceState); 33 setContentView(R.layout.main); 34 gridView= (GridView) findViewById(R.id.gridView); 35 datalist=new ArrayList<Map<String,Object>>(); 36 simpleAdapter=new SimpleAdapter(this,getData(),R.layout.item,new String[]{"pic","text"},new int[]{R.id.image,R.id.text}); 37 gridView.setAdapter(simpleAdapter); 38 gridView.setOnItemClickListener(this); 39 } 40 private List<Map<String,Object>> getData(){ 41 42 for(int i=0;i<icon.length;i++){ 43 Map<String,Object> map=new HashMap<String, Object>(); 44 map.put("pic",icon[i]); 45 map.put("text",iconName[i]); 46 datalist.add(map); 47 } 48 return datalist; 49 } 50 51 @Override 52 public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 53 Toast.makeText(this,iconName[position],Toast.LENGTH_SHORT).show();/*pisition从0开始计数*/ 54 } 55 }
item.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:gravity="center" 6 android:background="@color/black"> 7 <ImageView 8 android:src="@drawable/ic_launcher" 9 android:layout_width="60dp" 10 android:layout_height="60dp" 11 android:id="@+id/image" 12 /> 13 <TextView 14 android:layout_marginTop="5dp" 15 android:layout_width="wrap_content" 16 android:layout_height="wrap_content" 17 android:id="@+id/text" 18 android:textColor="@color/white" 19 android:textSize="20sp" 20 android:text="文字"/> 21 </LinearLayout>
main.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" android:layout_width="match_parent" 4 android:layout_height="match_parent"> 5 <GridView 6 android:layout_width="wrap_content" 7 android:layout_height="wrap_content" 8 android:id="@+id/gridView" 9 android:numColumns="3" 10 android:horizontalSpacing="10dp" 11 android:verticalSpacing="10dp" 12 13 ></GridView> 14 </LinearLayout> 15 <!-- android:numColumns="auto_fit" 每一行显示多少列,auto_fit自动适应 16 android:horizontalSpacing="10dp" 两列之间的间距 17 android:verticalSpacing="10dp" 两行之间的间距-->
标签:
原文地址:http://www.cnblogs.com/633sylss/p/5460507.html