视图实现gridview+adapter 图片加文字
1 这是实现的adapter类
?
?
package com.hao.test;
?
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
?
public class gridadapter extends BaseAdapter {
? ? private LayoutInflater inflater;
? ? String[] name;
? ? int[] iconarray;
?
? ? public gridadapter(Context context, String[] name, int[] iconarray) {
? ? ? ? this.inflater = LayoutInflater.from(context);
? ? ? ? this.name = name;
? ? ? ? this.iconarray = iconarray;
? ? }
?
? ? @Override
? ? public int getCount() {
? ? ? ? return name.length;
? ? }
?
? ? @Override
? ? public Object getItem(int position) {
? ? ? ? return position;
? ? }
?
? ? @Override
? ? public long getItemId(int position) {
? ? ? ? return position;
? ? }
?
? ? @Override
? ? public View getView(int position, View convertView, ViewGroup parent) {
? ? ? ? ViewHolder holder;
? ? ? ? if (convertView==null) {
? ? ? ? ? ? holder=new ViewHolder();
? ? ? ? ? ? convertView=this.inflater.inflate(R.layout.main_grid_item, null);
? ? ? ? ? ? holder.iv=(ImageView) convertView.findViewById(R.id.main_grid_item_iv);
? ? ? ? ? ? holder.tv=(TextView) convertView.findViewById(R.id.main_grid_item_tv);
? ? ? ? ? ? convertView.setTag(holder);
? ? ? ? }
? ? ? ? else {
? ? ? ? ? ?holder=(ViewHolder) convertView.getTag();
? ? ? ? }
? ? ? ? holder.iv.setImageResource(iconarray[position]);
? ? ? ? holder.tv.setText(name[position]);
? ? ? ? return convertView;
? ? }
? ? private class ViewHolder{
? ? ? ? ImageView iv;
? ? ? ? TextView tv;
? ? }
?
}
?
?
2这是要显示的格式main_grid_item
?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:gravity="center"
android:layout_width="wrap_content" android:layout_height="wrap_content">
<ImageView android:id="@+id/main_grid_item_iv"
android:layout_width="69dip" android:layout_height="69dip" />
<TextView android:id="@+id/main_grid_item_tv"
android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center_horizontal"
android:textStyle="bold" android:textColor="#333333" ?/>
</LinearLayout>
?
?
3 这个是activity中要显示的main.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"
? ? >
<GridView android:id="@+id/main_menu_grid_view"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:numColumns="auto_fit" android:padding="10dp"
android:verticalSpacing="20dp" android:horizontalSpacing="10dp"
android:columnWidth="75dp" android:stretchMode="columnWidth"
android:gravity="center" />
</LinearLayout>
?
4 这个是主activity?
?
package com.hao.test;
?
import android.app.Activity;
import android.app.AlertDialog;
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;
?
public class Test extends Activity {
? ? public GridView gridView;
?
? ? String aString = "timu";
?
? ? private String[] name = {
? ? ? ? ? ? aString, aString, aString, aString, aString, aString, aString, aString
? ? };
?
? ? private int[] iconarray = {
? ? ? ? ? ? R.drawable.icon, R.drawable.icon, R.drawable.icon, R.drawable.icon, R.drawable.icon,
? ? ? ? ? ? R.drawable.icon, R.drawable.icon, R.drawable.icon
? ? };
?
? ? @Override
? ? public void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.main);
? ? ? ? init();
? ? }
?
? ? private void init() {
? ? ? ? // TODO Auto-generated method stub
? ? ? ? gridView = (GridView) findViewById(R.id.main_menu_grid_view);
? ? ? ? gridadapter adapter = new gridadapter(this, name, iconarray);
? ? ? ? gridView.setAdapter(adapter);
? ? ? ? gridView.setOnItemClickListener(new OnItemClickListener() {
?
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
? ? ? ? ? ? ? ? // TODO Auto-generated method stub
? ? ? ? ? ? ? ? switch (position) {
? ? ? ? ? ? ? ? ? ? case 0:
? ? ? ? ? ? ? ? ? ? ? ? new AlertDialog.Builder(Test.this)
?
? ? ? ? ? ? ? ? ? ? ? ? .setTitle("Question")
?
? ? ? ? ? ? ? ? ? ? ? ? .setMessage("0")
?
? ? ? ? ? ? ? ? ? ? ? ? .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
?
? ? ? ? ? ? ? ? ? ? ? ? ? ? public void onClick(DialogInterface dialog, int whichButton) {
?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? setResult(RESULT_OK);
?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? finish();
?
? ? ? ? ? ? ? ? ? ? ? ? ? ? }
?
? ? ? ? ? ? ? ? ? ? ? ? })
?
? ? ? ? ? ? ? ? ? ? ? ? .setNegativeButton("No", new DialogInterface.OnClickListener() {
?
? ? ? ? ? ? ? ? ? ? ? ? ? ? public void onClick(DialogInterface dialog, int whichButton) {
?
? ? ? ? ? ? ? ? ? ? ? ? ? ? }
?
? ? ? ? ? ? ? ? ? ? ? ? })
?
? ? ? ? ? ? ? ? ? ? ? ? .show();
? ? ? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? ? ? case 1:
? ? ? ? ? ? ? ? ? ? ? ? new AlertDialog.Builder(Test.this)
?
? ? ? ? ? ? ? ? ? ? ? ? .setTitle("Question")
?
? ? ? ? ? ? ? ? ? ? ? ? .setMessage("1")
?
? ? ? ? ? ? ? ? ? ? ? ? .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
?
? ? ? ? ? ? ? ? ? ? ? ? ? ? public void onClick(DialogInterface dialog, int whichButton) {
?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? setResult(RESULT_OK);
?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? finish();
?
? ? ? ? ? ? ? ? ? ? ? ? ? ? }
?
? ? ? ? ? ? ? ? ? ? ? ? })
?
? ? ? ? ? ? ? ? ? ? ? ? .setNegativeButton("No", new DialogInterface.OnClickListener() {
?
? ? ? ? ? ? ? ? ? ? ? ? ? ? public void onClick(DialogInterface dialog, int whichButton) {
?
? ? ? ? ? ? ? ? ? ? ? ? ? ? }
?
? ? ? ? ? ? ? ? ? ? ? ? })
?
? ? ? ? ? ? ? ? ? ? ? ? .show();
? ? ? ? ? ? ? ? ? ? ? ? break;
?
? ? ? ? ? ? ? ? ? ? default:
? ? ? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? });
? ? }
?
}
?