android 有事件处理的网格视图用BaseAdapter实现

android 有事件处理的网格视图用BaseAdapter实现_第1张图片

 

 

 

在main.xml中

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

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

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:orientation="vertical" >

 

    <GridView

        android:id="@+id/myGridView"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:layout_marginTop="15dp"

        android:numColumns="3"

        android:stretchMode="columnWidth"/>

 

</LinearLayout>

 

 

在MyGridViewDemo.java程序中

package com.tarena.grideview;

 

import java.text.DateFormat.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.widget.AdapterView;

import android.widget.AdapterView.OnItemClickListener;

import android.widget.ArrayAdapter;

import android.widget.GridView;

import android.widget.ImageView;

import android.widget.LinearLayout;

import android.widget.LinearLayout.LayoutParams;

import android.widget.SimpleAdapter;

 

public class MyGridViewDemo extends Activity {

  private GridView myGridView = null;   //取得组件

  private int[] picRes = new int[]{R.drawable.ispic_1,

       R.drawable.ispic_2,R.drawable.ispic_3,R.drawable.ispic_4,

       R.drawable.ispic_5,R.drawable.ispic_6,R.drawable.ispic_7,

       R.drawable.ispic_8,R.drawable.ispic_9,R.drawable.ispic_10,

       R.drawable.ispic_11,R.drawable.ispic_12};

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        super.setContentView(R.layout.main);

        this.myGridView = (GridView)super.findViewById(R.id.myGridView);

        this.myGridView.setAdapter(new ImageAdapter(this,this.picRes));

        this.myGridView.setOnItemClickListener(new OnItemClickListenerImpl());

    }

   

    private class OnItemClickListenerImpl implements OnItemClickListener{

 

     public void onItemClick(AdapterView<?> parent, View view, int position,

         long id) {

       ImageView showImg = new ImageView(MyGridViewDemo.this);

       showImg.setScaleType(ImageView.ScaleType.CENTER);  //图片居中对齐

       showImg.setLayoutParams(new LinearLayout.LayoutParams(

           LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));

           showImg.setImageResource(MyGridViewDemo.this.picRes[position]);  //设置显示图片

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

         .setIcon(R.drawable.ic_launcher).setTitle("查看图片")

         .setView(showImg).setNegativeButton("关闭",new DialogInterface.OnClickListener() {

           

           public void onClick(DialogInterface dialog, int which) {

              // TODO Auto-generated method stub

             

            }

         }).create();

       dialog.show();

     }

   

    }

   

}

 

 

 

新建一个显示内容的类ImageAdapter.java

package com.tarena.grideview;

 

import android.content.Context;

import android.view.View;

import android.view.ViewGroup;

import android.widget.BaseAdapter;

import android.widget.ImageView;

//这是一个设置内容并显示的适配器,但是这个显示的内容需要由外部所决定

public class ImageAdapter extends BaseAdapter {

 

  private Context context = null;

  private int picIds[] = null;

  public ImageAdapter(Context context,int picIds[]){

     this.context = context;

     this.picIds = picIds;

  }

  public int getCount() {

     return this.picIds.length;

  }

 

  public Object getItem(int position) {

     return this.picIds[position];

  }

 

  public long getItemId(int position) {

     return this.picIds[position];

  }

 

  public View getView(int position, View convertView, ViewGroup parent) {

     ImageView img = new ImageView(this.context);

     img.setImageResource(this.picIds[position]);

     img.setScaleType(ImageView.ScaleType.CENTER);

     return img;

  }

 

}



 

你可能感兴趣的:(android 有事件处理的网格视图用BaseAdapter实现)