Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 高級開發 >> Android系列教程:GridView組件的使用

Android系列教程:GridView組件的使用

編輯:高級開發

 我新建了一個android技術交流群,群號為86686524,感興趣的可以申請加入,大家一塊學習。

  新建一個HelloGridVIEw的工程

  修改main.XML代碼如下:

  XML代碼

  < ?XML version="1.0" encoding="utf-8"?>

  < 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:horizontalSpacing="10dp"

  android:verticalSpacing="10dp"

  android:stretchMode="columnWidth"

  android:gravity="center">

  < /GridVIEw>

  < ?XML version="1.0" encoding="utf-8"?>

  < 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:horizontalSpacing="10dp"

  android:verticalSpacing="10dp"

  android:stretchMode="columnWidth"

  android:gravity="center">

  < /GridVIEw>

  很簡單,就是一個GridVIEw。設置列寬為90dp,這樣當我們的numColumns設置為auto_fit時,Android就會自動計算我們手機屏幕的大小以決定每一行展示幾個元素。這是很方便。android:horizontalSpacing定義列之間的間隔,android:verticalSpacing定義行之間的間隔。android:stretchMode設置為columnWidth是意味著根據列寬自動縮放。

  修改我們Activity HelloGridVIEw如下:

  Java代碼

  /**

  *

  * @author 飛雪無情

  * @since 2011-1-20

  */

  public class HelloGridVIEw extends Activity {

  /** Called when the activity is first created. */

  @Override

  protected void onCreate(Bundle savedInstanceState) {

  接上頁

  super.onCreate(savedInstanceState);

  setContentVIEw(R.layout.main);

  GridView gridView=(GridView)findViewById(R.id.gridvIEw);

  gridVIEw.setAdapter(new ImageAdapter(this));

  //單擊GridVIEw元素的響應

  gridVIEw.setOnItemClickListener(new OnItemClickListener() {

  @Override

  public void onItemClick(AdapterView parent, View vIEw,

  int position, long id) {

  //彈出單擊的GridVIEw元素的位置

  Toast.makeText(HelloGridVIEw.this,mThumbIds[position], Toast.LENGTH_SHORT).show();

  }

  });

  }

  private class ImageAdapter extends BaseAdapter{

  private Context mContext;

  public ImageAdapter(Context context) {

  this.mContext=context;

  }

  @Override

  public int getCount() {

  return mThumbIds.length;

  }

  @Override

  public Object getItem(int position) {

  return mThumbIds[position];

  }

  @Override

  public long getItemId(int position) {

  // TODO Auto-generated method stub

  return 0;

  }

  @Override

  public View getView(int position, View convertView, VIEwGroup parent) {

  //定義一個ImageView,顯示在GridVIEw裡

  ImageView imageVIEw;

  if(convertVIEw==null){

  imageView=new ImageVIEw(mContext);

  imageView.setLayoutParams(new GridVIEw.LayoutParams(85, 85));

  imageView.setScaleType(ImageVIEw.ScaleType.CENTER_CROP);

  imageVIEw.setPadding(8, 8, 8, 8);

  }else{

  imageView = (ImageView) convertVIEw;

  }

  imageVIEw.setImageResource(mThumbIds[position]);

  return imageVIEw;

  }

  }

  //展示圖片

  private Integer[] mThumbIds = {

  R.drawable.sample_2, R.drawable.sample_3,

  接上頁

  R.drawable.sample_4, R.drawable.sample_5,

  R.drawable.sample_6, R.drawable.sample_7,

  R.drawable.sample_0, R.drawable.sample_1,

  R.drawable.sample_2, R.drawable.sample_3,

  R.drawable.sample_4, R.drawable.sample_5,

  R.drawable.sample_6, R.drawable.sample_7,

  R.drawable.sample_0, R.drawable.sample_1,

  R.drawable.sample_2, R.drawable.sample_3,

  R.drawable.sample_4, R.drawable.sample_5,

  R.drawable.sample_6, R.drawable.sample_7

  };

  }

  /**

  *

  * @author 飛雪無情

  * @since 2011-1-20

  */

  public class HelloGridVIEw extends Activity {

  /** Called when the activity is first created. */

  @Override

  protected void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);

  setContentVIEw(R.layout.main);

  GridView gridView=(GridView)findViewById(R.id.gridvIEw);

  gridVIEw.setAdapter(new ImageAdapter(this));

  //單擊GridVIEw元素的響應

  gridVIEw.setOnItemClickListener(new OnItemClickListener() {

  @Override

  public void onItemClick(AdapterView parent, View vIEw,

  int position, long id) {

  //彈出單擊的GridVIEw元素的位置

  Toast.makeText(HelloGridVIEw.this,mThumbIds[position], Toast.LENGTH_SHORT).show();

  }

  });

  }

  private class ImageAdapter extends BaseAdapter{

  private Context mContext;

  public ImageAdapter(Context context) {

  this.mContext=context;

  }

  @Override

  public int getCount() {

  return mThumbIds.length;

  }

  @Override

  public Object getItem(int position) {

  return mThumbIds[position];

  }

  @Override

  public long getItemId(int position) {

  接上頁

  // TODO Auto-generated method stub

  return 0;

  }

  @Override

  public View getView(int position, View convertView, VIEwGroup parent) {

  //定義一個ImageView,顯示在GridVIEw裡

  ImageView imageVIEw;

  if(convertVIEw==null){

  imageView=new ImageVIEw(mContext);

  imageView.setLayoutParams(new GridVIEw.LayoutParams(85, 85));

  imageView.setScaleType(ImageVIEw.ScaleType.CENTER_CROP);

  imageVIEw.setPadding(8, 8, 8, 8);

  }else{

  imageView = (ImageView) convertVIEw;

  }

  imageVIEw.setImageResource(mThumbIds[position]);

  return imageVIEw;

  }

  }

  //展示圖片

  private Integer[] mThumbIds = {

  R.drawable.sample_2, R.drawable.sample_3,

  R.drawable.sample_4, R.drawable.sample_5,

  R.drawable.sample_6, R.drawable.sample_7,

  R.drawable.sample_0, R.drawable.sample_1,

  R.drawable.sample_2, R.drawable.sample_3,

  R.drawable.sample_4, R.drawable.sample_5,

  R.drawable.sample_6, R.drawable.sample_7,

  R.drawable.sample_0, R.drawable.sample_1,

  R.drawable.sample_2, R.drawable.sample_3,

  R.drawable.sample_4, R.drawable.sample_5,

  R.drawable.sample_6, R.drawable.sample_7

  };

  }

  這裡我們也是采用的自定義Adapter,可以上一節的自定義Adapter講解。展示了一些圖片,然後點擊一個圖片的時候會顯示這個圖片所在的位置。

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved