第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

全部開發(fā)者教程

Android 入門教程

菜單類控件
菜單:Menu
并發(fā)編程
多線程

Android 適配器 Adapter

本節(jié)將會引入一個全新的概念——適配器,這個名字很形象,和電源適配器的功能類似,從程序設(shè)計的角度出發(fā),它可以將不同類型、不同結(jié)構(gòu)的數(shù)據(jù)適配到一起。
在 Android 中,適配器是 UI 組件和數(shù)據(jù)之間的橋梁,它幫助我們將數(shù)據(jù)填充到 UI 組件當中,實現(xiàn)了一個典型的 MVC 模式。我們可以分別編寫獨立的 UI 樣式和數(shù)據(jù)模型,至于數(shù)據(jù)如何與 UI 組件綁定都由 Adapter 幫我們完成,這樣的好處就是做到 UI 和數(shù)據(jù)的解耦。Android 系統(tǒng)為我們提供了多種 Adapter,今天就來介紹幾種常見同場景下 Adapter 的基本用法。

1. 為什么要用 Adapter

我們首先看看 Android 為什么要引入 Adapter,也就是使用 Adapter 有哪些好處?
在 Android 中Adapter 通常是搭配列表控件使用,我們先看看在沒有學習 Adapter 的時候,如何實現(xiàn)一個列表樣式,我們可能需要以下幾步:

  1. 創(chuàng)建一個 ScrollView(上一節(jié)剛學到的,不熟悉的可以參照 22 節(jié));
  2. 在 ScrollView 中放置多個 View / ViewGroup,比如 TextView;
  3. 獲取每個 TextView 實例,根據(jù)業(yè)務(wù)需求為 TextView 設(shè)置 Text;
  4. 編寫額外代碼管理所有的 TextView,并且需要分辨點擊事件發(fā)生在第幾行從而定位到相應的 TextView,從而相應列表的點擊事件。

讀到這里,腦海里已經(jīng)有實現(xiàn)思路了嗎?即使你能捋清思路,代碼也很難寫的優(yōu)雅,因為編寫 TextView 樣式的這些 UI 代碼一定會和 TextView 內(nèi)容的數(shù)據(jù)代碼耦合在一起,這樣如何 UI 樣式一變,數(shù)據(jù)也需要做很大的調(diào)整,后期的維護成本是相當高的。最好的辦法就是能夠有一套邏輯專門去管理數(shù)據(jù)和 UI 代碼的綁定關(guān)系,用它來將 UI、Data 隔離開,提高代碼的簡潔性和可維護性。
我們結(jié)合一張圖來理解一下 Adapter:

Adapter in Android

電源適配器將電器和電源接口適配到一起,好處是可以讓手機等電子產(chǎn)品及家用電器廠商在生產(chǎn)過程中完全不需要考慮用戶電源接口的類型,可以是 220V 交流電、也可以是 USB 接口,適配工作只需要交給相應的 Adapter 就可以完成。而 Android 適配器是將數(shù)據(jù)和 UI 適配到一起,好處同樣也是我們在做 UI 的時候,完全不用考慮未來填充的數(shù)據(jù)是什么樣的,只需要針對不同的數(shù)據(jù)類型提供一個 Adapter 即可。

如果你覺得上面的描述都太抽象,后面可以通過幾個簡單的例子來直觀感受一下 Adapter 的用法。

2. Adapter 的類型

就像電源適配器需要根據(jù)不同的電源接口類型提供不同的適配器一樣,Android 中我們需要根據(jù)不同數(shù)據(jù)類型提供不同的 Adapter,系統(tǒng)已經(jīng)為我們實現(xiàn)了幾種 Adapter:

  1. BaseAdapter:
    所有 Adapter 的基類,通常我們需要實現(xiàn)自定義 Adapter 時,需要實現(xiàn)此抽象類,在實際開發(fā)中使用的最多的類型。
  2. ArrayAdapter:
    適用于一個單項列表,并且數(shù)據(jù)可以以數(shù)據(jù)形式存放的場景。
  3. SimpleAdapter:
    適用于一個列表項中有多個數(shù)據(jù)的場景,它可以將一個 map 里的數(shù)據(jù)映射到 xml 布局文件中的各個控件上。
  4. SimpleCursorAdapter:
    針對數(shù)據(jù)庫使用的 Adapter,使用場景很少。

3. 常見 Adapter 的用法

其實最常用的是 BaseAdapter,在實際開發(fā)中稍微復雜一點的列表都需要通過繼承 BaseAdapter 來編寫一個自定義的 Adapter 。大多數(shù)場景是結(jié)合 ListView / GridView 來完成,所以 BaseAdapter 的具體用法我們會放到后面 ListView / GridView 的相關(guān)章節(jié)做詳細介紹,這里主要是讓大家對 Adapter 的概念有個基本認識即可。

3.1 ArrayAdapter 的用法

ArrayAdpater 的用法非常簡單,如上一小節(jié)所說,它適合列表是單項列表并且數(shù)據(jù)可以存在一個數(shù)據(jù)當中的場景。首先我們創(chuàng)建布局文件,里面只需要存放一個 ListView 控件即可:

<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/simpleListView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:divider="#000"
    android:dividerHeight="2dp" />

其中有兩個屬性大家可能比較陌生:

 android:divider="#000"
 android:dividerHeight="2dp"

這兩個屬性是用來設(shè)置列表項之間的分割線樣式的,詳細的會在 ListView 章節(jié)進行介紹。然后還需要編寫列表中每個列表項的布局樣式,我們只需要一個 TextView 來顯示文本,而文本的內(nèi)容就是數(shù)組的數(shù)據(jù),列表項布局代碼 list_view.xml 如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/textView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:padding="30dp"
        android:textColor="#000" />
</LinearLayout>

一個我們非常熟悉的 TextView,然后就可以在 Java 代碼中通過 ArrayAdapter進行數(shù)據(jù) / UI 的綁定了,Java 代碼如下:

package com.emercy.myapplication;

import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import android.app.Activity;

public class MainActivity extends Activity {
    ListView mList;
    String mNums[] = {"TextView", "EditText", "Button", "ImageButton", "RadioButton", "ToggleButton",
            "ImageView", "ProgressBar", "SeekBar", "RatingBar", "ScrollView", "Adapter"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mList = findViewById(R.id.simpleListView);

        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(this, R.layout.list_view, R.id.textView, mNums);
        mList.setAdapter(arrayAdapter);
    }

}

我們在 OnCreate() 中獲取ListView對象,然后創(chuàng)建 ArrayAdapter,傳入列表項的布局文件 ID、需要顯示內(nèi)容的 TextView 控件 ID 以及數(shù)組形式的數(shù)據(jù)。最后通過 setAdapter 完成數(shù)據(jù)及 UI 的綁定,這樣系統(tǒng)就會幫我們完成適配工作,效果如下:

ArrayAdapter

我們寫在數(shù)組中的數(shù)據(jù)就會按順序填充到列表中了。

3.2 SimpleAdapter 的用法

SimpleAdapter 相比 ArrayAdapter 會更豐富一點,主要體現(xiàn)在 ArrayAdapter 只能適用于列表中只有一項數(shù)據(jù)(上一小節(jié)中的 TextView)的場景,而如果列表項由多個數(shù)據(jù)組成,比如文字配圖片的形式 ArrayAdapter 就有些力不從心,這時候就需要用到 SimpleAdapter 了。
整個 Activity 的布局文件依舊不變,只需要放置一個 ListView 即可。我們在之前的list_view.xml中增加一個 ImageView,如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="10dp"
        android:padding="5dp" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:padding="30dp"
        android:textColor="#000" />
</RelativeLayout>

從上面的布局文件可以看出,我們現(xiàn)在的列表項由兩個部分組成:一個圖片和一個文本。接著修改 Java 代碼,主要是數(shù)據(jù)格式的變換,現(xiàn)在數(shù)據(jù)數(shù)組需要包含圖片資源和文本內(nèi)容兩個部分,如下:

package com.emercy.myapplication;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;

public class MainActivity extends Activity {

    ListView mListView;
    String[] mDataName = {"蘋果", "梨", "香蕉", "桃子", "西瓜", "荔枝", "橘子"};
    int[] mDataImage = {R.drawable.apple, R.drawable.pear, R.drawable.banana, R.drawable.peach,
            R.drawable.watermelon, R.drawable.lychee, R.drawable.orange, R.drawable.orange};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mListView = findViewById(R.id.simpleListView);

        // 將水果圖片和水果名稱整合到一個map當中,最后將所有的水果都存放到ArrayList
        ArrayList<HashMap<String, String>> arrayList = new ArrayList<>();
        for (int i = 0; i < mDataName.length; i++) {
            HashMap<String, String> hashMap = new HashMap<>();
            hashMap.put("name", mDataName[i]);
            hashMap.put("image", mDataImage[i] + "");
            arrayList.add(hashMap);
        }
        String[] from = {"name", "image"};
        int[] to = {R.id.textView, R.id.imageView};
        SimpleAdapter simpleAdapter = new SimpleAdapter(this, arrayList, R.layout.list_view, from, to);
        mListView.setAdapter(simpleAdapter);

        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Toast.makeText(getApplicationContext(), mDataName[i], Toast.LENGTH_LONG).show();
            }
        });
    }
}

在這段例子中,我們使用兩個數(shù)組分別保存水果名稱及水果圖片,然后再將每個水果的名稱和圖片存入一個 map,接著把所有的水果 map 都整合到一個 ArrayList 當中,最后創(chuàng)建 SimpleAdapter,這一步也是最關(guān)鍵的。我們來單獨看看 SimpleAdapter 的創(chuàng)建語句:

SimpleAdapter simpleAdapter = new SimpleAdapter(this, arrayList, R.layout.list_view, from, to);

SimpleAdapter 構(gòu)造器參數(shù)比較多,我們來仔細分析分析。傳入構(gòu)造器的第二個參數(shù)是數(shù)據(jù)源,也就是存放所有水果 map 的 ArrayList 對象;傳入的第三個參數(shù)是列表項的布局文件,即 list_view.xml;第四個參數(shù)是一個字符串數(shù)組,表示水果 map 中的 key,也就是水果名和水果圖片的 key,用來與具體的 UI 控件對應;最后一個參數(shù)是一個整形數(shù)組,用來與第四個參數(shù)匹配,告訴系統(tǒng) map 中的哪些數(shù)據(jù)需要顯示到哪個 View 上。這樣一來,就完成了列表、列表項、數(shù)據(jù)的對應關(guān)系,接著直接用setAdapter完成適配,最后通過 ListView 的setOnItemClickListener為每個列表項添加點擊事件(具體使用方法會在 ListView 章節(jié)詳細介紹),效果如下:

SimpleAdapter

4 小結(jié)

本節(jié)介紹了一個比較新鮮的概念——適配器,大家初期理解它可以當成電源適配器來理解就好。然后介紹了幾種常用的使用方法,系統(tǒng)也為我們提供了幾種封裝好的 Adapter 可以應付一些簡單的場景。但是在大家實際的開發(fā)過程中能夠直接使用系統(tǒng)提供的 Adapter 的場景比較少,大多數(shù)情況還是要繼承 BaseAdapter 來自己實現(xiàn)一套 Adapter,這個內(nèi)容會在 ListView / GridView 相關(guān)章節(jié)做具體的介紹。另外,大家可以思考一下本章節(jié)的例子如果使用 ScrollView 要怎么實現(xiàn),優(yōu)劣勢在哪里?