GridViewActivity.java
package com.tsots.GridView_DialogWithPhoto; import android.app.Activity; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.GridView; import android.widget.ImageView; import android.widget.Toast; public class PhotoActivity extends Activity { Context context = PhotoActivity.this; ImageAdapter adapter = new ImageAdapter(this); /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); GridView gridview = (GridView) findViewById(R.id.gridview1); gridview.setNumColumns(3); gridview.setAdapter(adapter); gridview.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView parent, View v, final int position, long id) { ImageView iv = new ImageView(context); iv.setImageResource(adapter.mThumbIds[position]); new AlertDialog.Builder(context) .setTitle("Choose the Photo?") .setView(iv) .setPositiveButton("YES", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { Toast.makeText(context, "你選到了第"+(position+1)+"張圖", Toast.LENGTH_SHORT).show(); } }) .setNegativeButton("NO", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { } }) .show(); } }); } }
我們預先將所需的圖檔放入/res/draeable/,命名pic_01.png~pic_34.png
由GridView.LayoutParams(110, 110)可以設定顯示圖片的寬高
ImageAdapter.java
package com.tsots.GridView_ToastWithPhoto; import android.content.Context; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.GridView; import android.widget.ImageView; public class ImageAdapter extends BaseAdapter { private Context mContext; public ImageAdapter(Context c) { mContext = c; } public int getCount() { return mThumbIds.length; } public Object getItem(int position) { return null; } public long getItemId(int position) { return 0; } // create a new ImageView for each item referenced by the Adapter public View getView(int position, View convertView, ViewGroup parent) { ImageView imageView; if (convertView == null) { imageView = new ImageView(mContext); imageView.setLayoutParams(new GridView.LayoutParams(110, 110)); imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); imageView.setPadding(8, 8, 8, 8); } else { imageView = (ImageView) convertView; } imageView.setImageResource(mThumbIds[position]); return imageView; } public Integer[] mThumbIds = { R.drawable.pic_01, R.drawable.pic_02, R.drawable.pic_03, R.drawable.pic_04, R.drawable.pic_05, R.drawable.pic_06, R.drawable.pic_07, R.drawable.pic_08, R.drawable.pic_09, R.drawable.pic_10, R.drawable.pic_11, R.drawable.pic_12, R.drawable.pic_13, R.drawable.pic_14, R.drawable.pic_15, R.drawable.pic_16, R.drawable.pic_17, R.drawable.pic_18, R.drawable.pic_19, R.drawable.pic_20, R.drawable.pic_21, R.drawable.pic_22, R.drawable.pic_23, R.drawable.pic_24, R.drawable.pic_25, R.drawable.pic_26, R.drawable.pic_27, R.drawable.pic_28, R.drawable.pic_29, R.drawable.pic_30, R.drawable.pic_31, R.drawable.pic_32, R.drawable.pic_33, R.drawable.pic_34 }; }
沒有留言:
張貼留言