BaseAdapter就Android應用程序中經常用到的基礎數據適配器,它的主要用途是將一組數據傳到像ListView、Spinner、Gallery及GridView等UI顯示組件,它是繼承自接口類Adapter。(轉自寧 靜 致 遠http://www.cnblogs.com/mandroid/archive/2011/04/05/2005525.html)
1、Adapter類簡介
1)、Adapter相關類結構如下圖所示:
自定義Adapter子類,就需要實現上面幾個方法,其中最重要的是getView()方法,它是將獲取數據後的View組件返回,如ListView中每一行裏的TextView、Gallery中的每個ImageView。
2)、Adapter在Android應用程序中起着非常重要的作用,應用也非常廣泛,它可看作是數據源和UI組件之間的橋樑,其中Adapter、數據和UI之間的關係,可以用下圖表示:
3)、常用子類
2、BaseAdapter簡介
BaseAdapter是實現了ListAdapter和SpinnerAdapter兩個接口,當然它也可以直接給ListView和Spinner等UI組件直接提供數據。
相關類結構如下圖所示:
繼承BaseAdapter之後,需要重寫getCount,getItem,getItemId,getView四個方法,
其中最重要的方法爲getView方法,調用過程如下:
首先調用getCount方法,獲得Item個數,之後每繪製一次Item就調用一次getView方法,
通過此方法將事先定義好的XML作爲顯示效果,並且返回一個View對象作爲Item顯示出來,
這也是完成了適配器的主要轉換功能。getItem和getItemId方法會在調用ListView響應被
調用,如沒有完成getItemId方法的功能實現的話,當調用ListView的getItemIdAtPosition
方法時將會得不到想 要的結果,因爲該方法就是調用了對應的適配器的getItemId方法。
ListView無法獲得焦點問題:
ListView中很容易預見一個問題就是,Item點擊沒有反應,無法獲取焦點,原因多半是由於在你自己
定義的Item中存在諸如ImageButton,Button,CheckBox等子控件(也可以說是Button或者Checkable的
子類控件),此時這些子控件會將焦點獲取到,所以常常當點擊item時變化的是子控件,item本身的點擊沒有響應。
這時候需要使用descendantFocusability來解決問題了。API描述如下:
android:descendantFocusability
Defines the relationship between the ViewGroup and its descendants when looking for a View to take focus.
Must be one of the following constant values.
該屬性是當一個爲view獲取焦點時,定義viewGroup和其子控件兩者之間的關係。
屬性的值有三種:
beforeDescendants:viewgroup會優先其子類控件而獲取到焦點
afterDescendants:viewgroup只有當其子類控件不需要獲取焦點時才獲取焦點
blocksDescendants:viewgroup會覆蓋子類控件而直接獲得焦點
通常我們用到的是第三種,即在Item佈局的根佈局加上android:descendantFocusability=”blocksDescendants”的屬性就好了
3、示例
示例一:Gallery顯示一組圖片
運行結果:
說明:上面一行圖片是Gallery畫廊,每次點擊一個Gallery圖片時,會同時在下面以大圖形式顯示出來該圖片
佈局文件:
<?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" > <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.view.View.OnClickListener; import android.widget.AdapterView; import android.widget.BaseAdapter; import android.widget.Gallery; import android.widget.ImageView; 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}; /** Called when the activity is first created. */ @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中,方便訪問MainActivity中的各個變量,特別是imgs數組 private Context 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);//針對外面傳遞過來的Context變量, iv.setImageResource(imgs[position]); Log.i("magc", String.valueOf(imgs[position])); iv.setLayoutParams(new Gallery.LayoutParams(80, 80));//設置Gallery中每一個圖片的大小爲80*80。 iv.setScaleType(ImageView.ScaleType.FIT_XY); return iv; } } }