Android——使用Spinner实现下拉列表

时间:2021-07-13 10:12:04

1.执行步骤:

(1)确定数据源(添加一个下拉列表项的list)

(2).定义适配器,添加数据源(为下拉列表定义一个数组适配器ArrayAdapter)

(3)为适配器设置下拉时的菜单样式

adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
(4)将适配器添加到下拉列表上

spinner.setAdapter(adapter);
(5)为下拉列表设置各种事件的响应,这个事件响应菜单被选中

spinner.setOnItemSelectedListener(this);

2.用数组适配器ArrayAdapter实现下拉列表

activity_main.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="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textColor="#ff0000"/>

<Spinner
android:id="@+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content">

</Spinner>
</LinearLayout>

MainActivity.java

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends Activity implements AdapterView.OnItemSelectedListener {

private TextView textView;
private Spinner spinner;
private List<String> list;
private ArrayAdapter<String> adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

textView = (TextView) findViewById(R.id.textview);
spinner = (Spinner) findViewById(R.id.spinner);
textView.setText("您选择的城市是北京");
//1.设置数据源
list = new ArrayList<>();
list.add("北京");
list.add("上海");
list.add("广州");
list.add("深圳");

//2.定义适配器
adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,list);

//3.adapter设置下拉样式
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

//4.spinner加载适配器
spinner.setAdapter(adapter);

//5.spinner设置监听器
spinner.setOnItemSelectedListener(this);
}

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String cityName = adapter.getItem(position);
// String cityName = list.get(position);
textView.setText("您选择的城市是" + cityName);
}

@Override
public void onNothingSelected(AdapterView<?> parent) {
}
}

3.用简单适配器SimpleAdapter实现下拉列表

simple.xml

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

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/seclet_image"
android:layout_width="60dp"
android:layout_height="60dp" />
<TextView
android:id="@+id/select_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>

<Spinner
android:id="@+id/simple_spinner"
android:layout_width="wrap_content"
android:layout_height="60dp">
</Spinner>

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

<ImageView
android:id="@+id/image"
android:layout_width="40dp"
android:layout_height="40dp" />

<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ff0000"
android:textSize="20sp"/>

</LinearLayout>

SimpleActivity.java

package com.example.sx.spinner;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.SimpleAdapter;
import android.widget.Spinner;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* Created by sx on 2016/8/17.
*/
public class SimpleActivity extends Activity implements AdapterView.OnItemSelectedListener{

private ImageView selectImage;
private TextView selectText;
private Spinner simpleSpinner;
private List<Map<String,Object>> dataList;
private SimpleAdapter adapter;

private int[] imageList = {R.drawable.beijing,R.drawable.shanghai,R.drawable.guangzhou,R.drawable.shenzhen};
private String[] textList = {"北京","上海","广州","深圳"};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.simple);

selectImage = (ImageView) findViewById(R.id.seclet_image);
selectText = (TextView) findViewById(R.id.select_text);
simpleSpinner = (Spinner) findViewById(R.id.simple_spinner);

//1.设置数据源
dataList = new ArrayList<Map<String,Object>>();

//2.定义适配器
adapter = new SimpleAdapter(this,getData(),R.layout.item,new String[]{"image","text"},new int[]{R.id.image,R.id.text});

//3.adapter设置下拉样式,这里样式不要加,因为它并不支持图片和文字一起显示的样式
// adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

//4.spinner加载适配器
simpleSpinner.setAdapter(adapter);

//5.spinner设置监听器
simpleSpinner.setOnItemSelectedListener(this);
}

private List<Map<String,Object>> getData() {
Map<String,Object> map;
for (int i = 0; i < textList.length; i++) {
map = new HashMap<>();
map.put("image",imageList[i]);
map.put("text",textList[i]);
dataList.add(map);
}
return dataList;
}

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
selectImage.setImageResource(imageList[position]);
selectText.setText(textList[position]);
}

@Override
public void onNothingSelected(AdapterView<?> parent) {
}
}