說到Android GridView Listview一定會提到他的adapter,現整理如下:
main.xml布局裡面:
- <GridView
- android:id="@+id/grid"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:columnWidth="70dp"
- android:numColumns="auto_fit"
- android:verticalSpacing="10dp"
- android:horizontalSpacing="10dp"
- android:stretchMode="columnWidth"
- android:gravity="center"
- />
gridview_item.xml:
- <?xml version="1.0" encoding="utf-8"?>
- <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_height="wrap_content"
- android:layout_width="wrap_content"
- android:orientation="vertical"
- android:gravity="center"
- android:layout_gravity="center"
- >
- <LinearLayout
- android:layout_height="wrap_content"
- android:layout_width="wrap_content"
- android:orientation="vertical"
- android:gravity="center"
- android:layout_gravity="center">
- <ImageView
- android:id="@+id/grid_icon"
- android:layout_height="80dip"
- android:layout_width="80dip"
- android:gravity="center"
- android:layout_gravity="center"
- >
- </ImageView>
- <TextView
- android:id="@+id/grid_name"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:singleLine="true"
- android:gravity="center"
- android:layout_gravity="center"
- android:textSize="13px"
- android:layout_marginTop="4dip"
- >
- </TextView>
- </LinearLayout>
- </FrameLayout>
java 代碼:
- GridView grid = (GridView) findViewById(R.id.grid);
- grid.setAdapter(new GridAdapter(this, mNameList, mDrawableList));
GridAdapter.java類:
- package com.android.test;
-
- import java.util.ArrayList;
-
- import android.content.Context;
- import android.graphics.drawable.Drawable;
- import android.view.Gravity;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.BaseAdapter;
- import android.widget.ImageView;
- import android.widget.LinearLayout;
- import android.widget.TextView;
-
- public class GridAdapter extends BaseAdapter {
- private ArrayList<String> mNameList = new ArrayList<String>();
- private ArrayList<Drawable> mDrawableList = new ArrayList<Drawable>();
- private LayoutInflater mInflater;
- private Context mContext;
- LinearLayout.LayoutParams params;
-
- public GridAdapter(Context context, ArrayList<String> nameList, ArrayList<Drawable> drawableList) {
- mNameList = nameList;
- mDrawableList = drawableList;
- mContext = context;
- mInflater = LayoutInflater.from(context);
-
- params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
- params.gravity = Gravity.CENTER;
- }
-
- public int getCount() {
- return mNameList.size();
- }
-
- public Object getItem(int position) {
- return mNameList.get(position);
- }
-
- public long getItemId(int position) {
- return position;
- }
-
- public View getView(int position, View convertView, ViewGroup parent) {
- ItemViewTag viewTag;
-
- if (convertView == null)
- {
- convertView = mInflater.inflate(R.layout.gridview_item, null);
-
- // construct an item tag
- viewTag = new ItemViewTag((ImageView) convertView.findViewById(R.id.grid_icon), (TextView) convertView.findViewById(R.id.grid_name));
- convertView.setTag(viewTag);
- } else
- {
- viewTag = (ItemViewTag) convertView.getTag();
- }
-
- // set name
- viewTag.mName.setText(mNameList.get(position));
-
- // set icon
- viewTag.mIcon.setBackgroundDrawable(mDrawableList.get(position));
- viewTag.mIcon.setLayoutParams(params);
- return convertView;
- }
-
- class ItemViewTag
- {
- protected ImageView mIcon;
- protected TextView mName;
-
- /**
- * The constructor to construct a navigation view tag
- *
- * @param name
- * the name view of the item
- * @param size
- * the size view of the item
- * @param icon
- * the icon view of the item
- */
- public ItemViewTag(ImageView icon, TextView name)
- {
- this.mName = name;
- this.mIcon = icon;
- }
- }
-
- }
到此,adapter的使用就算結束,效果圖如下:
更多Android相關信息見Android 專題頁面 http://www.linuxidc.com/topicnews.aspx?tid=11