前言 最近一直在講AndroidUI的開發(fā),今天講一下Spinner控件,這是一個(gè)列表選擇框,可以彈出一個(gè)列表供用戶選擇。在本片博客中,會(huì)講解Spinner的基本屬性以及設(shè)置之后的效果,以及使用SimpleAdapter綁定自定義格式的數(shù)據(jù)到Spinner中。 Spinner Spinner 是一個(gè)列表選擇框,會(huì)在用戶選擇后,展示一個(gè)列表供用戶進(jìn)行選擇。Spinner是ViewGroup的間接子類,它和其他的Android控件一樣,數(shù)據(jù)需要使用Adapter進(jìn)行封裝。 下面介紹一下Spinner的常用XML屬性,Android也為其屬性提供了相應(yīng)的getter、setter方法:
作為一個(gè)列表選擇控件,Spinner具有一些選中選項(xiàng)可以觸發(fā)的事件,但它本身沒有定義這些事件,均繼承自間接父類 AdapterView 。Spinner支持的幾個(gè)常用事件有以下幾個(gè):
PS:因?yàn)檫m配器可以設(shè)置各種不同的樣式,有選擇、單選、多選,所以O(shè)nItemCLickListener和OnItemSelectedListener是適用于不同場景的。 Spinner的數(shù)據(jù)綁定 對于Spinner展示的數(shù)據(jù)源,一般使用兩種方式設(shè)定數(shù)據(jù):
先來講講通過XML資源文件設(shè)置Spinner數(shù)據(jù)的方式,首先需要在/res/values目錄下新建XML格式的資源文件,名字不重要,但是一般會(huì)使用strings.xml。在其中的<resourse.../>標(biāo)簽下,定義<string-array.../>標(biāo)簽,通過它中的<item.../>標(biāo)簽來設(shè)置選擇數(shù)據(jù)。 XML文件結(jié)構(gòu): <resource> <string-array name="arrayname"> <item>item1</item> <item>item2</item> <item>item3</item> </string-array> <resource> 通過適配器Adapter可以設(shè)定比較復(fù)雜的展示效果,一般項(xiàng)目中比較常用的也是這種方式。但是如果對于動(dòng)態(tài)的、簡單的數(shù)據(jù),可以使用ArrayAdapter對象來設(shè)置適配器,關(guān)于ArrayAdapter類的介紹,在我的另外一篇博客中有介紹,不了解的朋友可以先看看: Android--UI之AutoCompleteTextView 。 下面通過一個(gè)示例,講解一下上面說的屬性、事件,以及使用ArrayAdapter和XML資源文件設(shè)定簡單數(shù)據(jù),代碼中注釋已經(jīng)說的很清楚了,這里就不再累述了。 布局代碼: 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas./apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" > 6 7 <TextView 8 android:layout_width="wrap_content" 9 android:layout_height="wrap_content" 10 android:text="彈出的Spinner" /> 11 12 <Spinner 13 android:id="@+id/spinnerBase" 14 android:layout_width="match_parent" 15 android:layout_height="wrap_content" 16 android:spinnerMode="dialog" /> 17 18 <TextView 19 android:layout_width="wrap_content" 20 android:layout_height="wrap_content" 21 android:text="下拉的Spinner(默認(rèn))" /> 22 23 <Spinner 24 android:id="@+id/spinnerBase1" 25 android:layout_width="match_parent" 26 android:layout_height="wrap_content" 27 android:spinnerMode="dropdown" /> 28 29 <TextView 30 android:layout_width="wrap_content" 31 android:layout_height="wrap_content" 32 android:text="entries綁定數(shù)據(jù)源" /> 33 34 <Spinner 35 android:id="@+id/spinnerBase2" 36 android:layout_width="match_parent" 37 android:layout_height="wrap_content" 38 android:entries="@array/beijing" /> 39 40 <TextView 41 android:layout_width="wrap_content" 42 android:layout_height="wrap_content" 43 android:text="彈出帶標(biāo)題的Dialog,并且使用entries綁定數(shù)據(jù)源" /> 44 45 <Spinner 46 android:id="@+id/spinnerBase3" 47 android:layout_width="match_parent" 48 android:layout_height="wrap_content" 49 android:entries="@array/beijing" 50 android:prompt="@string/beij_prompt" 51 android:spinnerMode="dialog" /> 52 53 </LinearLayout> 實(shí)現(xiàn)代碼: 1 package com.bgxt.datatimepickerdemo; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 import android.app.Activity; 7 import android.os.Bundle; 8 import android.view.View; 9 import android.widget.AdapterView; 10 import android.widget.AdapterView.OnItemSelectedListener; 11 import android.widget.ArrayAdapter; 12 import android.widget.Spinner; 13 import android.widget.Toast; 14 15 public class SpinnerBaseActivity extends Activity { 16 private Spinner spinner1, spinner2; 17 18 @Override 19 protected void onCreate(Bundle savedInstanceState) { 20 super.onCreate(savedInstanceState); 21 setContentView(R.layout.activity_spinnerbase); 22 23 spinner1 = (Spinner) findViewById(R.id.spinnerBase); 24 spinner2 = (Spinner) findViewById(R.id.spinnerBase1); 25 // 聲明一個(gè)ArrayAdapter用于存放簡單數(shù)據(jù) 26 ArrayAdapter<String> adapter = new ArrayAdapter<String>( 27 SpinnerBaseActivity.this, android.R.layout.simple_spinner_item, 28 getData()); 29 // 把定義好的Adapter設(shè)定到spinner中 30 spinner1.setAdapter(adapter); 31 spinner2.setAdapter(adapter); 32 // 為第一個(gè)Spinner設(shè)定選中事件 33 spinner1.setOnItemSelectedListener(new OnItemSelectedListener() { 34 35 @Override 36 public void onItemSelected(AdapterView<?> parent, View view, 37 int position, long id) { 38 // 在選中之后觸發(fā) 39 Toast.makeText(SpinnerBaseActivity.this, 40 parent.getItemAtPosition(position).toString(), 41 Toast.LENGTH_SHORT).show(); 42 } 43 44 @Override 45 public void onNothingSelected(AdapterView<?> parent) { 46 // 這個(gè)一直沒有觸發(fā),我也不知道什么時(shí)候被觸發(fā)。 47 //在官方的文檔上說明,為back的時(shí)候觸發(fā),但是無效,可能需要特定的場景 48 } 49 }); 50 51 } 52 53 private List<String> getData() { 54 // 數(shù)據(jù)源 55 List<String> dataList = new ArrayList<String>(); 56 dataList.add("北京"); 57 dataList.add("上海"); 58 dataList.add("南京"); 59 dataList.add("宜昌"); 60 return dataList; 61 } 62 63 } XML資源文件: 1 <?xml version="1.0" encoding="utf-8"?> 2 <resources> 3 <string name="app_name">SpinnerDemo</string> 4 <string name="action_settings">Settings</string> 5 <string name="hello_world">Hello world!</string> 6 <string name="beij_prompt">北京區(qū)域</string> 7 <string-array name="beijing"> 8 <item>朝陽區(qū)</item> 9 <item>海淀區(qū)</item> 10 <item>房山區(qū)</item> 11 <item>豐臺(tái)區(qū)</item> 12 <item>東城區(qū)</item> 13 <item>西城區(qū)</item> 14 </string-array> 15 </resources> 效果展示,圖片順序,從上到下:
SimpleAdapter配置Spinner數(shù)據(jù) 對于一個(gè)稍復(fù)雜的數(shù)據(jù),如果想對其展示,光使用ArrayAdapter是無法滿足需求的,現(xiàn)在在另外介紹一個(gè)Adapter, SimpleAdapter ,同樣繼承自Adapter。 SimpleAdapter是一個(gè)簡單的適配器,映射靜態(tài)的XML格式的布局文件到視圖中??梢灾付ㄒ粋€(gè)List<Map<P,T>>格式的數(shù)據(jù),List中的每一條數(shù)據(jù)對應(yīng)一行,而Map中的每一條數(shù)據(jù)對應(yīng)數(shù)據(jù)行的一列。這個(gè)數(shù)據(jù)用來映射到XML定義的布局控件中,對應(yīng)關(guān)系通過構(gòu)造函數(shù)的另外兩個(gè)參數(shù)來指定,現(xiàn)在來介紹一下SimpleAdapter的構(gòu)造函數(shù)。 SimpleAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to)
下面通過一個(gè)示例講解一下SimpleAdapter是如何設(shè)置自定義格式數(shù)據(jù)的。 布局代碼: 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas./apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" > 6 7 <Spinner android:id="@+id/spinnerAdapter" android:layout_width="match_parent" 8 android:layout_height="wrap_content" /> 9 </LinearLayout> XML布局資源代碼: 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas./apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="wrap_content" 5 android:orientation="horizontal" > 6 7 <ImageView 8 android:id="@+id/imageview" 9 android:layout_width="60dp" 10 android:layout_height="60dp" 11 android:paddingLeft="10dp" 12 android:src="@drawable/ic_launcher" /> 13 14 <TextView 15 android:id="@+id/textview" 16 android:layout_width="match_parent" 17 android:layout_height="wrap_content" 18 android:gravity="center_vertical" 19 android:paddingLeft="10dp" 20 android:textColor="#000" 21 android:textSize="16dp" /> 22 23 </LinearLayout> 實(shí)現(xiàn)代碼: 1 package com.bgxt.datatimepickerdemo; 2 3 import java.util.ArrayList; 4 import java.util.HashMap; 5 import java.util.List; 6 import java.util.Map; 7 8 import android.app.Activity; 9 import android.os.Bundle; 10 import android.view.View; 11 import android.widget.AdapterView; 12 13 import android.widget.AdapterView.OnItemSelectedListener; 14 import android.widget.SimpleAdapter; 15 import android.widget.Spinner; 16 import android.widget.Toast; 17 18 public class SpinnerAdapterActivity extends Activity { 19 private Spinner spinner; 20 21 @Override 22 protected void onCreate(Bundle savedInstanceState) { 23 // TODO Auto-generated method stub 24 super.onCreate(savedInstanceState); 25 setContentView(R.layout.activity_spinneradapter); 26 27 spinner = (Spinner) findViewById(R.id.spinnerAdapter); 28 //聲明一個(gè)SimpleAdapter獨(dú)享,設(shè)置數(shù)據(jù)與對應(yīng)關(guān)系 29 SimpleAdapter simpleAdapter = new SimpleAdapter( 30 SpinnerAdapterActivity.this, getData(), R.layout.items, 31 new String[] { "ivLogo", "applicationName" }, new int[] { 32 R.id.imageview, R.id.textview }); 33 //綁定Adapter到Spinner中 34 spinner.setAdapter(simpleAdapter); 35 //Spinner被選中事件綁定。 36 spinner.setOnItemSelectedListener(new OnItemSelectedListener() { 37 38 @Override 39 public void onItemSelected(AdapterView<?> parent, View view, 40 int position, long id) { 41 //parent為一個(gè)Map結(jié)構(gòu)的和數(shù)據(jù) 42 Map<String, Object> map = (Map<String, Object>) parent 43 .getItemAtPosition(position); 44 Toast.makeText(SpinnerAdapterActivity.this, 45 map.get("applicationName").toString(), 46 Toast.LENGTH_SHORT).show(); 47 } 48 49 @Override 50 public void onNothingSelected(AdapterView<?> arg0) { 51 52 } 53 }); 54 } 55 56 public List<Map<String, Object>> getData() { 57 //生成數(shù)據(jù)源 58 List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); 59 //每個(gè)Map結(jié)構(gòu)為一條數(shù)據(jù),key與Adapter中定義的String數(shù)組中定義的一一對應(yīng)。 60 Map<String, Object> map = new HashMap<String, Object>(); 61 map.put("ivLogo", R.drawable.bmp1); 62 map.put("applicationName", "表情1"); 63 list.add(map); 64 Map<String, Object> map2 = new HashMap<String, Object>(); 65 map2.put("ivLogo", R.drawable.bmp2); 66 map2.put("applicationName", "表情2"); 67 list.add(map2); 68 Map<String, Object> map3 = new HashMap<String, Object>(); 69 map3.put("ivLogo", R.drawable.bmp3); 70 map3.put("applicationName", "表情3"); 71 list.add(map3); 72 return list; 73 } 74 } 效果展示:
請支持原創(chuàng),尊重原創(chuàng),轉(zhuǎn)載請注明出處。謝謝。 |
|