Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發實例

Android 基礎類別之 BaseAdapter 詳解

編輯:Android 開發實例

BaseAdapter 是 Android 應用程式中經常使用的基礎資料適配器,主要用途是將一組資料傳遞給 ListView、Spinner、Gallery 及 GridView 等 UI 顯示元件。它繼承自 Adapter 介面。

一、Adapter 類別簡介

自訂 Adapter 子類別時,需要實作幾個關鍵方法,其中最重要的是 getView() 方法——它負責將取得資料後的 View 元件回傳,例如 ListView 每一行裡的 TextView、Gallery 中的每個 ImageView。

Adapter 在 Android 應用中扮演著非常重要的角色,應用也相當廣泛,可視為資料源與 UI 元件之間的橋樑。

二、BaseAdapter 簡介

BaseAdapter 實作了 ListAdapter 與 SpinnerAdapter 兩個介面,因此能直接為 ListView 與 Spinner 等 UI 元件提供資料。

三、示例:Gallery 顯示一組圖片

以下範例中,上方是 Gallery 畫廊,每次點擊一個 Gallery 圖片時,會同時在下方以大圖形式顯示該圖片。

配置檔案:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 >
<TextView 
 android:layout_width="fill_parent" 
 android:layout_height="wrap_content" 
 android:text="@string/hello"
 />

<Gallery 
 android:id="@+id/gallery1" 
 android:layout_width="match_parent" 
 android:spacing="5px" 
 android:layout_height="wrap_content"
 ></Gallery>
<ImageView
 android:id="@+id/iv"
 android:layout_gravity="center_vertical"
 android:layout_marginTop="20px"
 android:layout_width="320px"
 android:layout_height="320px"
 ></ImageView>

</LinearLayout>

MainActivity 類別:

package com.magc.adapter;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

public class MainActivity extends Activity {
 private Gallery gallery;
 private ImageView imgview;
 private int[] imgs = {R.drawable.a6,R.drawable.a1,R.drawable.a2,R.drawable.a3,R.drawable.a4,R.drawable.a5};
 @Override
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.main);
 imgview = (ImageView)findViewById(R.id.iv);
 gallery = (Gallery)findViewById(R.id.gallery1);
 MyImgAdapter adapter = new MyImgAdapter(this);
 gallery.setAdapter(adapter);
 gallery.setOnItemClickListener(new OnItemClickListener() {
 //用戶點擊圖片時,將該圖片的 ResourceID 設到下面的 ImageView 中
 @Override
 public void onItemClick(AdapterView<?> arg0, View view, int position,
 long arg3) {
 imgview.setImageResource(imgs[position]);
 }
 });
 }

 class MyImgAdapter extends BaseAdapter {
 //自訂圖片 Adapter 以內部類別形式存在於 MainActivity 中,方便存取 imgs 陣列
 private Context context;
 public MyImgAdapter(Context context) {
 super();
 this.context = context;
 }

 @Override
 public int getCount() {
 return imgs.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) {
 //針對每一個圖片 ID 建立一個 ImageView 實例
 ImageView iv = new ImageView(context);
 iv.setImageResource(imgs[position]);
 Log.i("magc", String.valueOf(imgs[position]));
 iv.setLayoutParams(new Gallery.LayoutParams(80, 80));//設定每張圖片大小為 80*80
 iv.setScaleType(ImageView.ScaleType.FIT_XY);
 return iv;
 }
 }
}

透過自訂的 BaseAdapter,我們在 getView() 中為每個資料項建立對應的 ImageView,並在 getCount() 中回傳資料筆數,便能輕鬆地將一組圖片呈現在 Gallery 畫廊中,點擊後即於下方大圖顯示。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved