简单记录
程序结构为 activity main.xml cell.mxl 另外drawalbe文件夹下放置所需图片资源
主UI布局代码:
-
<LinearLayout xmlns:android=""
-
xmlns:tools=""
-
android:layout_width="fill_parent"
-
android:layout_height="fill_parent"
-
android:orientation="vertical">"
-
-
-
android:layout_width="fill_parent"
-
android:layout_height="wrap_content"
-
android:id="@+id/grid01"
-
android:horizontalSpacing="1pt"
-
android:verticalSpacing="1pt"
-
android:numColumns="4"
-
android:gravity="center"
-
/>
-
-
android:id="@+id/imageview"
-
android:layout_width="240dp"
-
android:layout_height="240dp"
-
android:layout_gravity="center_horizontal"
-
android:contentDescription="@string/app_name
Activity代码
-
package com.gridviewdemo;
-
-
import java.util.ArrayList;
-
import java.util.HashMap;
-
import java.util.List;
-
import java.util.Map;
-
-
-
-
import android.os.Bundle;
-
import android.app.Activity;
-
import android.view.Menu;
-
import android.view.View;
-
import android.widget.AdapterView;
-
import android.widget.AdapterView.OnItemClickListener;
-
import android.widget.GridView;
-
import android.widget.ImageView;
-
import android.widget.SimpleAdapter;
-
-
public class MainActivity extends Activity {
-
-
GridView grid;
-
ImageView imageview;
-
int [] imageIds =new int []{
-
R.drawable.img1,R.drawable.img2,
-
R.drawable.img3,R.drawable.img4,
-
R.drawable.img5,R.drawable.img6,
-
R.drawable.img6,R.drawable.ic_launcher
-
};
-
-
@Override
-
protected void onCreate(Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
-
setContentView(R.layout.main);
-
-
List<Map<String,Object>> listitems =new ArrayList<Map<String,Object>>();
-
for(int i=0;i<imageIds.length;i++){
-
Map<String,Object> listitem =new HashMap<String,Object>();
-
listitem.put("image", imageIds[i]);
-
listitems.add(listitem);
-
}
-
-
/**
-
* 本程序主要内容也是下面小段代码,所做的就是为网格布局构建一个Adapter
-
*/
-
imageview = (ImageView)findViewById(R.id.imageview);
-
SimpleAdapter simpleAdapter =new SimpleAdapter(this,
-
listitems,
-
R.layout.cell,
-
new String[]{"image"},
-
new int []{R.id.image1});
-
grid =(GridView)findViewById(R.id.grid01);
-
grid.setAdapter(simpleAdapter);
-
-
imageview.setImageResource(imageIds[7]);//默认显示的图片
-
-
grid.setOnItemClickListener(new OnItemClickListener(){
-
-
@Override
-
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
-
long arg3) {
-
// TODO Auto-generated method stub
-
imageview.setImageResource(imageIds[arg2]);
-
-
}
-
-
});
-
-
-
}
-
-
@Override
-
public boolean onCreateOptionsMenu(Menu menu) {
-
// Inflate the menu; this adds items to the action bar if it is present.
-
getMenuInflater().inflate(R.menu.main, menu);
-
return true;
-
}
-
-
}
另外Adapter所需的cell.xml文件内容为
-
<?xml version="1.0" encoding="UTF-8"?>
-
<LinearLayout xmlns:android=""
-
android:orientation="horizontal"
-
android:layout_width="fill_parent"
-
android:layout_height="fill_parent"
-
android:gravity="center_horizontal"
-
android:padding="2pt"
-
>
-
<ImageView
-
android:id="@+id/image1"
-
android:layout_width="50dp"
-
android:layout_height="50dp"
-
/>
-
</LinearLayout>
运行效果为:
阅读(3445) | 评论(0) | 转发(0) |