Android详细教程(基础篇):十七、View组件高级篇:GridView组件详解

版权声明:本文为博主原创文章,转载请注明出处。作者:杨雄进 https://blog.csdn.net/makyan/article/details/89173161

7.12. GridView(网格组件)

继承机构如下:

java.lang.Object

   

android.view.View

 

   

android.view.ViewGroup

 

 

   

android.widget.AdapterView<T extends android.widget.Adapter>

 

 

 

   

android.widget.AbsListView

 

 

 

 

   

android.widget.GridView

同样的,跟GralleryView组件相似,设置GridView中的内容使用适配器Adapter来完成,可以使用SimpleAdapter和BaseAdapter。

以下通过SimpleAdapter来完成。

配置:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout

     xmlns:android="http://schemas.android.com/apk/res/android"

     android:orientation="vertical"

     android:layout_width="fill_parent"

     android:layout_height="fill_parent">

     <GridView

          android:id="@+id/myGridView"

          android:layout_width="fill_parent"

          android:layout_height="wrap_content"

          android:numColumns="3"

          android:stretchMode="columnWidth"/>

</LinearLayout>

自定义组件:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout

     xmlns:android="http://schemas.android.com/apk/res/android"

     android:orientation="horizontal"

     android:layout_width="wrap_content"

     android:layout_height="wrap_content"

     android:background="#000000">

     <ImageView

          android:id="@+id/img"

          android:layout_width="wrap_content"

          android:layout_height="wrap_content"

          android:scaleType="center"

          android:padding="3px"/>

</LinearLayout>

点击图图片后显示的对话组件:

show_img.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout

     xmlns:android="http://schemas.android.com/apk/res/android"

     android:orientation="vertical"

     android:layout_width="fill_parent"

     android:layout_height="fill_parent">

     <ImageView

          android:id="@+id/showImg"

          android:layout_width="fill_parent"

          android:layout_height="fill_parent"/>

</LinearLayout>

Activity:

package com.makyan.demo;

import java.lang.reflect.Field;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import android.app.Activity;

import android.app.AlertDialog;

import android.app.Dialog;

import android.content.DialogInterface;

import android.os.Bundle;

import android.view.View;

import android.view.ViewGroup.LayoutParams;

import android.widget.AdapterView;

import android.widget.AdapterView.OnItemClickListener;

import android.widget.GridView;

import android.widget.ImageView;

import android.widget.LinearLayout;

import android.widget.SimpleAdapter;

public class GridActivity extends Activity {

     private List<Map<String,Integer>> list = new ArrayList<Map<String,Integer>>() ;

     private SimpleAdapter simpleAdapter = null;// 适配器

     private GridView myGridView = null ;// GridView组件

     @Override

     public void onCreate(Bundle savedInstanceState) {

          super.onCreate(savedInstanceState);

          super.setContentView(R.layout.activity_grid);// 调用布局管理器

          myGridView = (GridView) super.findViewById(R.id.myGridView) ;// 取得组件

          initAdapter() ; // 初始化适配器

          myGridView.setAdapter(this.simpleAdapter) ;   // 设置图片

          myGridView.setOnItemClickListener(new OnItemClickListenerImpl()) ;

     }

     private class OnItemClickListenerImpl implements OnItemClickListener {

          @SuppressWarnings("unchecked")

          @Override

          // 选项单击事件

          public void onItemClick(AdapterView<?> parent, View view, int position,long id) {

               // 定义图片组件

               ImageView showImg = new ImageView(GridActivity.this);

               // 居中显示

               showImg.setScaleType(ImageView.ScaleType.CENTER); 

               // 布局参数

               showImg.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,

                         LayoutParams.WRAP_CONTENT));                

               // 取出Map,怎样把放到适配器中的数据取出来

               Map<String, Integer> map = (Map<String, Integer>) simpleAdapter.getItem(position);

               // 设置显示图片

               showImg.setImageResource(map.get("img"));              

               // 创建Dialog

               Dialog dialog = new AlertDialog.Builder(GridActivity.this)  

                         .setIcon(R.drawable.pic_m)// 设置显示图片

                         .setTitle("查看图片")// 设置标题

                         .setView(showImg)// 设置组件

                         .setNegativeButton("关闭",// 设置取消按钮

                                   new DialogInterface.OnClickListener() {

                                        public void onClick(DialogInterface dialog,

                                                  int whichButton) {

                                        }}).create();  // 创建对话框

               dialog.show();// 显示对话框

          }

     }

     // 初始化适配器

     public void initAdapter(){                                           

          Field[] fields = R.drawable.class.getDeclaredFields();

          for (int x = 0; x < fields.length; x++) {   

               // 所有png_*命名的图片

               if (fields[x].getName().startsWith("png_")){

                    // 定义Map

                    Map<String,Integer> map = new HashMap<String,Integer>() ;   

                    try {

                         // 设置图片资源

                         map.put("img", fields[x].getInt(R.drawable.class)) ;

                    } catch (Exception e) {                               

                    }

                    // 保存Map

                    this.list.add(map) ;                             

               }

          }

          simpleAdapter = new SimpleAdapter(this, // 实例化SimpleAdapter

                    this.list, // 要包装的数据集合

                    R.layout.grid_layout, // 要使用的显示模板

                    new String[] { "img" }, // 定义要显示的Map的Key

                    new int[] {R.id.img });  // 与模板中的组件匹配

     }

}

猜你喜欢

转载自blog.csdn.net/makyan/article/details/89173161
今日推荐