Android控件之GridView用法实例分析

本文实例讲述了Android控件之GridView用法。分享给大家供大家参考。具体如下:

GridView是一项显示二维的viewgroup,可滚动的网格。一般用来显示多张图片。
以下模拟九宫图的实现,当鼠标点击图片时会进行相应的跳转链接。

目录结构如下:

Android控件之GridView用法实例分析_第1张图片

main.xml布局文件,存放GridView控件





night_item.xml布局文件,存放显示控件



  
  
  
  


strings.xml



  Hello World, GvActivity!
  九宫图
  跳转到TestActivity1
  跳转到TestActivity2
  跳转到TestActivity3


清单文件



  
    
      
        
        
      
    
    
    
    
  
  
 

跳转类TestActivity1、TestActivity2、TestActivity3

package com.ljq.gv;
import android.app.Activity;
import android.os.Bundle;
public class TestActivity1 extends Activity {
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.main);
  }
}
package com.ljq.gv;
import android.app.Activity;
import android.os.Bundle;
public class TestActivity2 extends Activity {
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.main);
  }
}
package com.ljq.gv;
import android.app.Activity;
import android.os.Bundle;
public class TestActivity3 extends Activity {
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.main);
  }
}

类GvActivity:

package com.ljq.gv;
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.GridView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
public class GvActivity extends Activity {
  private String texts[] = null;
  private int images[] = null;
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    images=new int[]{R.drawable.p1, R.drawable.p2,
        R.drawable.p3, R.drawable.p4, 
        R.drawable.p5,R.drawable.p6, 
        R.drawable.p7,R.drawable.p8};
    texts = new String[]{ "宫式布局1", "宫式布局2",
        "宫式布局3", "宫式布局4", 
        "宫式布局5", "宫式布局6",
        "宫式布局7", "宫式布局8"};
    GridView gridview = (GridView) findViewById(R.id.gridview);
    ArrayList> lstImageItem = new ArrayList>();
    for (int i = 0; i < 8; i++) {
      HashMap map = new HashMap();
      map.put("itemImage", images[i]);
      map.put("itemText", texts[i]);
      lstImageItem.add(map);
    }
    SimpleAdapter saImageItems = new SimpleAdapter(this, 
        lstImageItem,// 数据源
        R.layout.night_item,// 显示布局
        new String[] { "itemImage", "itemText" }, 
        new int[] { R.id.itemImage, R.id.itemText }); 
    gridview.setAdapter(saImageItems);
    gridview.setOnItemClickListener(new ItemClickListener());
  }
  class ItemClickListener implements OnItemClickListener {
    /**
     * 点击项时触发事件
     * 
     * @param parent 发生点击动作的AdapterView
     * @param view 在AdapterView中被点击的视图(它是由adapter提供的一个视图)。
     * @param position 视图在adapter中的位置。
     * @param rowid 被点击元素的行id。
     */
    public void onItemClick(AdapterView parent, View view, int position, long rowid) {
      HashMap item = (HashMap) parent.getItemAtPosition(position);
      //获取数据源的属性值
      String itemText=(String)item.get("itemText");
      Object object=item.get("itemImage");
      Toast.makeText(GvActivity.this, itemText, Toast.LENGTH_LONG).show();
      //根据图片进行相应的跳转
      switch (images[position]) {
      case R.drawable.p1:
        startActivity(new Intent(GvActivity.this, TestActivity1.class));//启动另一个Activity
        finish();//结束此Activity,可回收
        break;
      case R.drawable.p2:
        startActivity(new Intent(GvActivity.this, TestActivity2.class));
        finish();
        break;
      case R.drawable.p3:
        startActivity(new Intent(GvActivity.this, TestActivity3.class));
        finish();
        break;
      }
    }
  }
}

运行结果

Android控件之GridView用法实例分析_第2张图片

希望本文所述对大家的Android程序设计有所帮助。

你可能感兴趣的:(Android控件之GridView用法实例分析)