主GridView界面
<GridView
android:id="@+id/myGrid"android:layout_width="match_parent"
android:layout_height="wrap_content"
android:numColumns="auto_fit"
android:columnWidth="80dp"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:gravity="center"
></GridView>
每个GridView的格式
<ImageView
android:id="@+id/topimg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"
/>
<TextView
android:id="@+id/foottitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/topimg"
android:text="战友圈"
android:textColor="@android:color/white"
android:gravity="center"
android:layout_marginTop="10dp"
/>
设置关系的Activity
import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.content.Intent;
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;
public class GridViewActivity extends Activity {
private ArrayList<HashMap<String, Object>> data = new ArrayList<HashMap<String,Object>>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gridview);
GridView gridView = (GridView) findViewById(R.id.myGrid);
initData();
SimpleAdapter adapter = new SimpleAdapter(this,
data,
R.layout.gridview_item,
new String[]{"title","img"},
new int[]{R.id.foottitle,R.id.topimg});
gridView.setAdapter(adapter);
gridView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
String title = (String) data.get(arg2).get("title");
if(title.equals("微博")){
Intent intent = new Intent(GridViewActivity.this, ViewPagerActivity.class);
startActivity(intent);
}
}
});
}
private void initData(){
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("title", "百度地图");
map.put("img", R.drawable.icon_home_busy_bg);
data.add(map);
map = new HashMap<String, Object>();
map.put("title", "QQ");
map.put("img", R.drawable.icon_home_dynamic_bg);
data.add(map);
map = new HashMap<String, Object>();
map.put("title", "搜狐视频");
map.put("img", R.drawable.icon_home_weixiu_bg);
data.add(map);
map = new HashMap<String, Object>();
map.put("title", "酷狗音乐");
map.put("img", R.drawable.icon_home_used_bg);
data.add(map);
map = new HashMap<String, Object>();
map.put("title", "微博");
map.put("img", R.drawable.icon_home_zzj_bg);
data.add(map);
map = new HashMap<String, Object>();
map.put("title", "QQ空间");
map.put("img", R.drawable.icon_home_job_bg);
data.add(map);
}
}