gridview布局
item配件布局
主Activity
package com.su.testgrid3;import android.app.Activity;import android.os.Bundle;import android.os.Environment;import android.view.View;import android.widget.AdapterView;import android.widget.AdapterView.OnItemLongClickListener;import android.widget.GridView;import android.widget.Toast;import android.widget.AdapterView.OnItemClickListener;public class TestGrid3Activity extends Activity { private GridView gridView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); String[] titles = FileUtil.getImageNames("/mnt/sdcard/k/"); String[] imagePaths = new String[titles.length]; for (int i = 0; i < titles.length; i++) { imagePaths[i]="/mnt/sdcard/k/"+titles[i]; } gridView = (GridView) findViewById(R.id.gridview); PictureAdapter adapter = new PictureAdapter(titles, imagePaths, this); gridView.setAdapter(adapter); gridView.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView parent, View v, int position, long id) { Toast.makeText(TestGrid3Activity.this, "你点击了" + (position + 1), Toast.LENGTH_SHORT).show(); } }); gridView.setOnItemLongClickListener(new OnItemLongClickListener(){ public boolean onItemLongClick(AdapterView parent, View v, int position, long id) { Toast.makeText(TestGrid3Activity.this, "你长按了" + (position + 1), Toast.LENGTH_SHORT).show(); return true; /* * 这里需要true * 因为:OnItemLongClick事件中:down事件返回值标记此次事 * 件是否为点击事件(返回false,是点击事件;返回true,不记为点击事件), * 而up事件标记此次事件结束时间,也就是判断是否为长按。 */ } }); }}适配器
// 2012-4-18下午07:46:57package com.su.testgrid3;import java.util.ArrayList;import java.util.List;import android.content.Context;import android.graphics.BitmapFactory;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.ImageView;import android.widget.TextView;//自定义适配器class PictureAdapter extends BaseAdapter { private LayoutInflater inflater; private List
picture的javabean
// 2012-4-18下午07:47:44package com.su.testgrid3;class Picture { private String title; private String imageId; public Picture() { super(); } public Picture(String title, String imageId) { super(); this.title = title; this.imageId = imageId; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getImageId() { return imageId; } public void setImageId(String imageId) { this.imageId = imageId; }}
获取图片文件的名字数组的操作类
// 2012-4-18下午07:49:44 package com.su.testgrid3;import java.io.File;public class FileUtil { public static String[] getImageNames(String folderPath) { File file01 = new File(folderPath); String[] files01 = file01.list(); int imageFileNums = 0; for (int i = 0; i < files01.length; i++) { File file02 = new File(folderPath + "/" + files01[i]); if (!file02.isDirectory()) { if (isImageFile(file02.getName())) { imageFileNums++; } } } String[] files02 = new String[imageFileNums]; int j = 0; for (int i = 0; i < files01.length; i++) { File file02 = new File(folderPath + "/" + files01[i]); if (!file02.isDirectory()) { if (isImageFile(file02.getName())) { files02[j] = file02.getName(); j++; } } } return files02; } private static boolean isImageFile(String fileName) { String fileEnd = fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length()); if (fileEnd.equalsIgnoreCase("jpg")) { return true; } else if (fileEnd.equalsIgnoreCase("png")) { return true; } else if (fileEnd.equalsIgnoreCase("bmp")) { return true; } else { return false; } }}