android customAlertDialog + GridView
ImageAdapter 생성자에서 배열 넘겨주고 class를 공용으로 쓰게 만들수도 있고
showDialog(id) 에서 id넘겨주고 onDialogCreate(int id)에서 id값을 switch~case문으로 배열 구분할수 있게 해준다.
MainActivity
package com.example.gridexam;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private Button goBtn;
private Context mContext;
private Dialog mDialog;
private GridView grid;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext = this;
goBtn = (Button)findViewById(R.id.goBtn);
goBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
showDialog(0);
}
});
}
@Override
protected Dialog onCreateDialog(int id) {
// TODO Auto-generated method stub
//step 1:Load custom dialog layout
LayoutInflater myInflater = LayoutInflater.from(mContext);
View myView = myInflater.inflate(R.layout.customdialog, null);
GridView gv = (GridView) myView.findViewById(R.id.gridview);
//step 2: Set self-defined ImageAdaper to our gridview
gv.setAdapter(new ImageAdapter(mContext));
//step 3: Set up the behavior when user touches an item in the grid
gv.setOnItemClickListener(new GridView.OnItemClickListener(){
public void onItemClick(AdapterView<?> parent, View v,int position, long id)
{
// TODO Auto-generated method stub
Toast.makeText(v.getContext(), "Position is "+position, Toast.LENGTH_SHORT).show();
//remove this statement if you want keep the dialog after user touched
mDialog.dismiss();
}
});
//step 4: Set the custom view to the AlertDialog
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("choose a context");
builder.setView(myView);
mDialog = builder.create();
return mDialog;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public class ImageAdapter extends BaseAdapter {
private Context mContext;
private LayoutInflater mInflater;
public ImageAdapter(Context c) {
mInflater = LayoutInflater.from(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 index
public View getView(int position, View convertView, ViewGroup parent) {
//ViewHolder is a self-defined class, and every instance is consisted of an icon and a text
ViewHolder holder;
if (convertView == null) { // if it's not recycled,
convertView = mInflater.inflate(R.layout.customcontent, null);
//convertView.setLayoutParams(new GridView.LayoutParams(90, 90));
holder = new ViewHolder();
//set any text and pic on the views, they will be replaced later
holder.title = (TextView) convertView.findViewById(R.id.categoryText);
holder.icon = (ImageView )convertView.findViewById(R.id.categoryimage);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.icon.setAdjustViewBounds(true);
//holder.icon.setScaleType(ImageView.ScaleType.CENTER_CROP);
holder.icon.setPadding(8, 8, 8, 8);
holder.title.setText(categoryContent[position]);
holder.icon.setImageResource(mThumbIds[position]);
return convertView;
}
class ViewHolder {
TextView title;
ImageView icon;
}
// references to our images
private Integer[] mThumbIds = {
R.drawable.ic_launcher, R.drawable.ic_launcher,R.drawable.ic_launcher,
R.drawable.ic_launcher, R.drawable.ic_launcher,R.drawable.ic_launcher,
R.drawable.ic_launcher
};
private String[] categoryContent = {
"spider0", "yao1", "cat2",
"spider3", "yao4", "cat5",
"yao6"
};
}
}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/goBtn"
android:text="go!Ryu"
/>
</LinearLayout>
customdialog.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"/>
</LinearLayout>
customcontent.xml