Android使用GridView以表格的形式显示多张图片

                                    效果图

Android使用GridView以表格的形式显示多张图片_第1张图片

MainActivity

import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import java.util.HashMap; import java.util.Map; import android.widget.AdapterView.OnItemClickListener; import android.widget.GridView; import android.widget.SimpleAdapter; import android.widget.Toast; public class MainActivity extends Activity implements OnItemClickListener { private GridView gridView; private List> dataList; private int[] img = { R.drawable.item_post, R.drawable.bycicle}; private SimpleAdapter simpleAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); gridView = (GridView) findViewById(R.id.gridView1); dataList = new ArrayList>(); getData(); simpleAdapter = new SimpleAdapter(this, dataList, R.layout.otem_layout, new String[] { "picture", "text" }, new int[] { R.id.img, R.id.tv1 }); gridView.setAdapter(simpleAdapter); gridView.setOnItemClickListener(this); } private void getData() { for (int i = 0; i < 25; i ++) { Map map = new HashMap(); if(i%2==0){ map.put("picture" ,img[0]); } else { map.put( "picture",img[1] ); } map.put("text", i); dataList.add(map); } } @Override public void onItemClick(AdapterView parent, View arg1, int position, long id) { Toast.makeText(this, "点击了第"+position+"个", Toast.LENGTH_SHORT).show(); } 

activity_main_xml

item.xml

你可能感兴趣的:(Android使用GridView以表格的形式显示多张图片)