Android控件学习笔记之 GridView(实现九宫格)

实现九宫格

Main.java

 

package  cn.test;

import  android.app.Activity;
import  android.content.Intent;
import  android.os.Bundle;
import  android.util.Log;
import  android.view.View;
import  android.widget.Button;

public   class  Main  extends  Activity {
    
private  Button btn_viewItemAndText;
    
private  Button btn_viewGridViewList;
    @Override
    
public   void  onCreate(Bundle savedInstanceState) {
        
super .onCreate(savedInstanceState);
        setContentView(R.layout.main);
        initControl();
        setControlListener();
    }
    
    
/**
     * 初始化控件
     
*/
    
private   void  initControl(){
        btn_viewItemAndText
= (Button)findViewById(R.id.btn_viewItemAndText);
        btn_viewGridViewList
= (Button)findViewById(R.id.btn_viewGridViewList);
    }
    
    
/**
     * 增加控件监听
     
*/
    
private   void  setControlListener(){
        btn_viewItemAndText.setOnClickListener(onClickViewItemAndTextListener);
        btn_viewGridViewList.setOnClickListener(onCLickViewGridViewListListener);
    }
    
    
/**
     * 图文列表按钮事件
     
*/
    
private  View.OnClickListener onClickViewItemAndTextListener  =   new  View.OnClickListener() {
        @Override
        
public   void  onClick(View v) {
            Log.v(
" OnClick  " " btn_viewItemAndText " );
            Intent intent 
=   new  Intent(Main. this , ItemAndTextListView. class );
            startActivity(intent);
        }
    };
    
    
private  View.OnClickListener onCLickViewGridViewListListener = new  View.OnClickListener() {
        @Override
        
public   void  onClick(View v) {
            Log.v(
" OnClick  " " btn_viewGridViewList " );
            Intent intent 
=   new  Intent(Main. this , GridViewList. class );
            startActivity(intent);
        }
    };
}

 

GridViewList.java
package  cn.test;

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

import  android.app.Activity;
import  android.os.Bundle;
import  android.widget.GridView;
import  android.widget.SimpleAdapter;

public   class  GridViewList  extends  Activity {
    
private  GridView gridview;

    
public   void  onCreate(Bundle savedInstanceState) {
        
super .onCreate(savedInstanceState);
        setContentView(R.layout.gridview);
        initControl();
    }
    
    
/**
     * 初始加载控件
     
*/
    
private   void  initControl(){
        SimpleAdapter adapter 
=   new  SimpleAdapter( this , getData(), R.layout.gridviewitem,  new  String[]{ " ItemImage " " ItemText " },  new   int []{R.id.ItemImage, R.id.ItemText});
        gridview 
=  (GridView)findViewById(R.id.gridView1);
        gridview.setAdapter(adapter);
    }

    
/**
     * 模拟数据
     * 
@return
     
*/
    
private  List < Map < String, Object >>  getData() {
        List
< Map < String, Object >>  items  =   new  ArrayList < Map < String, Object >> ();
        
for  ( int  i  =   0 ; i  < 20 ; i ++ ) {
            Map
< String, Object >  item  =   new  HashMap < String, Object > ();
            item.put(
" ItemImage " , R.drawable.icon);
            item.put(
" ItemText " " 九宫项目 "   +  i);
            items.add(item);
        }
        
return  items;

    }
}

 

gridview.xml
<? xml version="1.0" encoding="utf-8" ?>
< LinearLayout
  
xmlns:android ="http://schemas.android.com/apk/res/android"
  android:orientation
="vertical"
  android:layout_width
="match_parent"
  android:layout_height
="match_parent" >

< GridView  android:layout_height ="wrap_content"  android:id ="@+id/gridView1"
    android:layout_width
="match_parent"  android:numColumns ="auto_fit"
    android:horizontalSpacing
="10dp"  android:verticalSpacing ="10dp"
    android:columnWidth
="90dp"  android:stretchMode ="columnWidth"
    android:gravity
="center" >

</ GridView >
    
</ LinearLayout >

 

 

gridviewitem.xml

 

<? xml version="1.0" encoding="utf-8" ?>
< RelativeLayout
  
xmlns:android ="http://schemas.android.com/apk/res/android"
  android:orientation
="vertical"
  android:layout_width
="match_parent"
  android:layout_height
="match_parent" >
    
< ImageView  android:layout_width ="wrap_content"
        android:layout_height
="wrap_content"  android:id ="@+id/ItemImage" ></ ImageView >
    
< TextView  android:layout_height ="wrap_content"  android:id ="@+id/ItemText"
        android:layout_width
="wrap_content"  android:layout_below ="@+id/ItemImage" ></ TextView >
    
</ RelativeLayout >

 

AndroidManifest.xml
<? xml version="1.0" encoding="utf-8" ?>
< manifest  xmlns:android ="http://schemas.android.com/apk/res/android"
      package
="cn.test"
      android:versionCode
="1"
      android:versionName
="1.0" >
    
< uses-sdk  android:minSdkVersion ="8"   />

    
< application  android:icon ="@drawable/icon"  android:label ="@string/app_name" >
        
< activity  android:name =".Main"
                  android:label
="@string/app_name" >
            
< intent-filter >
                
< action  android:name ="android.intent.action.MAIN"   />
                
< category  android:name ="android.intent.category.LAUNCHER"   />
            
</ intent-filter >
        
</ activity >
 
          
< activity  android:name =".ItemAndTextListView"
                  android:label
="@string/app_name" >
            
< intent-filter >
                
< action  android:name ="android.intent.action.itemAndTextListView"   />
                
< category  android:name ="android.intent.category.DEFAULT"   />
            
</ intent-filter >
        
</ activity >
         
< activity  android:name =".GridViewList"
                  android:label
="@string/app_name" >
            
< intent-filter >
                
< action  android:name ="android.intent.action.gridViewList"   />
                
< category  android:name ="android.intent.category.DEFAULT"   />
            
</ intent-filter >
        
</ activity >
    
</ application >
</ manifest >

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值