學(xué)習(xí)Android自定義Spinner適配器
本文為大家分享Android自定義Spinner適配器的相關(guān)知識點,供大家參考,具體內(nèi)容如下
一、大致效果

二.關(guān)鍵代碼
在注釋中講重點吧。
(1)Spinner的布局: car_brand_spinner.xml
即為彈出來的下拉列表的布局啦,后面的那個布局就不拿出來丟人現(xiàn)眼了,反正知道有一個Spinner的id為carBrandSpinner就可以了。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/car_brand_name"
android:layout_gravity="left"
android:layout_alignParentLeft="true"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/car_brand_flag"
android:layout_gravity="right"
android:layout_alignParentRight="true" />
</RelativeLayout>
</LinearLayout>
(2)適配器
ArrayAdapter carBrandAdapter=new ArrayAdapter<String>
(
AddCarActivity.this,
android.R.layout.simple_spinner_dropdown_item,
carBrandNameList//是String[],就是所有要顯示的brandName
){
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
convertView = View.inflate(AddCarActivity.this,R.layout.car_brand_spinner,null);//獲得Spinner布局View
if(convertView!=null)
{
TextView carBrandNameView = (TextView)convertView.findViewById(R.id.car_brand_name);
ImageView carBrandFlagView = (ImageView)convertView.findViewById(R.id.car_brand_flag);
try
{
JSONObject json = new JSONObject(carBrandList.get(position).get("carBrand").toString());
carBrandNameView.setText(json.getString("carBrandName"));//設(shè)置數(shù)據(jù),我這里的數(shù)據(jù)是從服務(wù)器讀出來的,所以前面有一個轉(zhuǎn)化取值的過程
}catch (Exception e){}
Bitmap bitmap =Common.String2Bitmap(carBrandList.get(position).get("carBrandFlagContent").toString());//這里也一樣,圖片數(shù)據(jù)來自于服務(wù)器,同時有一個將數(shù)據(jù)從String轉(zhuǎn)Bitmap的過程
if(bitmap!=null)
carBrandFlagView.setImageBitmap(bitmap);//顯示圖片
}
return convertView;
}
};
//給Spinner set適配器
Spinner carBrandSpinner=(Spinner)findViewById(R.id.carBrandSpinner);
carBrandSpinner.setAdapter(carBrandAdapter);
carBrandSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
@Override//重寫Item被選擇的事件
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {}
@Override
public void onNothingSelected(AdapterView<?> parent) {}
});
到此結(jié)束!
以上就是關(guān)于Android Spinner適配器的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助。
- Android中Spinner(下拉框)控件的使用詳解
- android 之Spinner下拉菜單實現(xiàn)級聯(lián)
- Android Spinner 下拉菜單的使用
- Android實現(xiàn)下拉菜單Spinner效果
- Android自定義Spinner下拉列表(使用ArrayAdapter和自定義Adapter實現(xiàn))
- Android下拉列表spinner的實例代碼
- Android列表選擇框Spinner使用方法詳解
- android應(yīng)用開發(fā)之spinner控件的簡單使用
- Android中使用Spinner實現(xiàn)下拉列表功能
- Android控件之Spinner用法實例分析
- Android列表控件Spinner簡單用法示例
相關(guān)文章
Android編程開發(fā)之ScrollView嵌套GridView的方法
這篇文章主要介紹了Android編程開發(fā)之ScrollView嵌套GridView的方法,結(jié)合實例分析了ScrollView嵌套GridView的相關(guān)注意事項與處理技巧,需要的朋友可以參考下2015-12-12
Android 實現(xiàn)IOS選擇拍照相冊底部彈出的實例
這篇文章主要介紹了Android 實現(xiàn)IOS選擇拍照相冊底部彈出的實例的相關(guān)資料,這里提供了實現(xiàn)效果圖及實現(xiàn)代碼,需要的朋友可以參考下2017-07-07
Android中Volley框架進(jìn)行請求網(wǎng)絡(luò)數(shù)據(jù)的使用
這篇文章主要介紹了Android中Volley框架進(jìn)行請求網(wǎng)絡(luò)數(shù)據(jù)的使用,本文給大家介紹的非常詳細(xì)具有參考借鑒價值,需要的朋友可以參考下2016-10-10

