日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

(转)Android基础类之BaseAdapter

發布時間:2023/12/9 Android 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 (转)Android基础类之BaseAdapter 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
BaseAdapter就Android應用程序中經常用到的基礎數據適配器,它的主要用途是將一組數據傳到像ListView、Spinner、Gallery及GridView等UI顯示組件,它是繼承自接口類Adapter, 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組件直接提供數據。 相關類結構如下圖所示: 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;
}


/*(non-Javadoc)
* @see android.widget.Adapter#getCount()
*/
@Override
public int getCount() {
return imgs.length;
}

/*(non-Javadoc)
* @see android.widget.Adapter#getItem(int)
*/
@Override
public Object getItem(int position) {
return position;
}

/*(non-Javadoc)
* @see android.widget.Adapter#getItemId(int)
*/
@Override
public long getItemId(int position) {
return position;
}

/*(non-Javadoc)
* @see android.widget.Adapter#getView(int, android.view.View, android.view.ViewGroup)
*/
@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;
}

}

} 示例2:通過一個提示框來選擇頭像的功能(Gallery和ImageSwitcher結合顯示圖片) 待續……

轉載于:https://www.cnblogs.com/xingmeng/archive/2012/04/16/2451540.html

總結

以上是生活随笔為你收集整理的(转)Android基础类之BaseAdapter的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。