Android 之 GrideView网格视图

阅读更多
GrideView网格视图:

子布局:


	

    
    



主布局:



程序入口:

package com.example.advancedview;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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.LinearLayout;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 网格视图  -- 用于显示图片 和 信息
 * @author Administrator
 *
 */
public class GridViewActivity extends Activity {
	
	private GridView grideView ; // 网格视图
	
	// 准备数据源
	private int[] images  = new int[]{
			R.drawable.liyu,
			R.drawable.jiangkui,
			R.drawable.liqingzhao,
			R.drawable.ouyangxiu,
			R.drawable.qinguan,
			R.drawable.sushi,
			R.drawable.wanganshi,
			R.drawable.xinqiji
	};
	private String[] names = new String[]{
			"理喻",
			"姜夔",
			"李清照",
			"欧阳修",
			"秦观",
			"苏轼",
			"王安石",
			"辛弃疾",
	};
	

	// 程序入口
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.grideview_layout);
		
//		// 获取布局文件中控件的对象
//		grideView = (GridView) findViewById(R.id.gridView); 
//		
//		// M 数据
//		List data = this.getData();
//		
//		// c 控制
//		SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.gride_item, new String[]{"image","name"}, new int[]{R.id.image,R.id.name});
//		
//		// V 视图
//		grideView.setAdapter(adapter);
//		
//		// 设置监听器
//		grideView.setOnItemClickListener(itemlistener);
		
		Intent intent = new Intent(GridViewActivity.this,GalleryActivity.class);
		startActivity(intent);
	}
	
	// 准备数据源
	public List> getData(){
		
		List> list = new ArrayList>();
		Map map = null;
		for (int i = 0; i < names.length; i++) {
			map = new HashMap();
			map.put("image", images[i]);
			map.put("name", names[i]);
			list.add(map);
		}
		return list;
	}
	
	// 网格视图点击子项事件
	private OnItemClickListener itemlistener = new OnItemClickListener() {

		public void onItemClick(AdapterView arg0, View arg1, int arg2,
				long arg3) {
			LinearLayout layout  =  (LinearLayout) arg0.getAdapter().getView(arg2, null, null);
			TextView msg = (TextView) layout.findViewById(R.id.name);
			String m = msg.getText().toString();
			Toast.makeText(GridViewActivity.this, ""+arg2, Toast.LENGTH_LONG).show();
		}
	};

}

你可能感兴趣的:(android,GrideView,网格视图)