編輯:Android 開發實例
BaseAdapter 是 Android 應用程式中經常使用的基礎資料適配器,主要用途是將一組資料傳遞給 ListView、Spinner、Gallery 及 GridView 等 UI 顯示元件。它繼承自 Adapter 介面。
自訂 Adapter 子類別時,需要實作幾個關鍵方法,其中最重要的是 getView() 方法——它負責將取得資料後的 View 元件回傳,例如 ListView 每一行裡的 TextView、Gallery 中的每個 ImageView。
Adapter 在 Android 應用中扮演著非常重要的角色,應用也相當廣泛,可視為資料源與 UI 元件之間的橋樑。
BaseAdapter 實作了 ListAdapter 與 SpinnerAdapter 兩個介面,因此能直接為 ListView 與 Spinner 等 UI 元件提供資料。
以下範例中,上方是 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 畫廊中,點擊後即於下方大圖顯示。
Android MediaPlayer 實作指南:從播放控制到進度條同步
要在 Android 上播放音訊或影片,MediaPlayer 是最不需要額外套件就能上手的方式。它被封裝在內建的媒體播放服務之上,音訊、影片與串流格式都能處理。 取得實
Android 仿今日頭條下拉導航菜單:PopupWindow 加 GridView 實作
本文完整示範如何在 Android 中做出「下拉導航選擇菜單」的效果,也就是點一下頂部標題,下方展開一排圖文並茂的分類選單。這種互動在新聞客戶端裡很常見,用來容納較多的分
用 MediaPlayer 做一個 Android 音樂播放器
要在 Android 上播放音訊或影片,最直接的方式就是走 MediaPlayer。它位於 android.media 套件,能播放應用程式內的資源、裝置上的檔案,也能處
Android JSON 解析入門:用 JSONObject 撈出你要的那幾個欄位
JSON(JavaScript Object Notation)自從被提出之後,因為格式輕量、可讀性高,很快就在 Web API 的世界裡取代了 XML 的位置。不管是瀏