当前位置:   article > 正文

【Android】-- 下拉列表Spinner、适配器Adapter_android spinner adapter

android spinner adapter

一、下拉列表 Spinner

Spinner用于从一串列表中选择某项,功能类似于单选按钮的组合。

例:下拉列表框

 XML文件

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="match_parent"
  3. android:layout_height="match_parent"
  4. android:orientation="vertical">
  5. <TextView
  6. android:layout_width="match_parent"
  7. android:layout_height="wrap_content"
  8. android:text="下拉列表框"/>
  9. <Spinner
  10. android:id="@+id/sp_dropdown"
  11. android:layout_width="match_parent"
  12. android:layout_height="wrap_content"
  13. android:spinnerMode="dropdown"/>
  14. <!-- 对话框模式将此改成dialog-->
  15. android:spinnerMode="dialog"/>
  16. </LinearLayout>

 java代码

  1. public class SpinnerDropdwnActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
  2. private Spinner sp_dropdown;
  3. // 定义下拉列表需要显示的文本数组
  4. private final static String[] starArray = {"天安门广场","天坛公园","故宫","北京动物园"};
  5. @Override
  6. protected void onCreate(Bundle savedInstanceState) {
  7. super.onCreate(savedInstanceState);
  8. setContentView(R.layout.activity_spinner_dropdwn);
  9. sp_dropdown = findViewById(R.id.sp_dropdown);
  10. // 声明数组适配器
  11. ArrayAdapter<String> startAdapter = new ArrayAdapter<>(this,R.layout.item_select,starArray);
  12. // 设置下拉框标题,对话框模式才显示
  13. sp_dropdown.setPrompt("请选择地点");
  14. sp_dropdown.setAdapter(startAdapter);
  15. // 设置默认显示第一项
  16. sp_dropdown.setSelection(0);
  17. // 给下拉框设置选择监听器,一旦用户选中某一项,就触发监听器的onItemSelected方法
  18. sp_dropdown.setOnItemSelectedListener(this);
  19. }
  20. @Override
  21. public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
  22. Toast.makeText(this,"你选择的是"+starArray[position],Toast.LENGTH_SHORT).show();
  23. }
  24. @Override
  25. public void onNothingSelected(AdapterView<?> adapterView) {
  26. }
  27. }

layout下创建item_select.xml文件

  1. <TextView xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="match_parent"
  3. android:layout_height="50dp"
  4. android:gravity="center"
  5. android:textColor="#0000ff"
  6. android:textSize="17sp"
  7. android:text="北京"/>

二、适配器Adapter

适配器负责从数据集合中取出对应的数据显示到条目布局上。

 1、简单适配器SimpleAdapter

SimpleAdapter允许在列表项中同时展示文本与图片

例:

 XML文件

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="match_parent"
  3. android:layout_height="match_parent"
  4. android:orientation="vertical">
  5. <TextView
  6. android:layout_width="match_parent"
  7. android:layout_height="wrap_content"
  8. android:text="简单适配器"
  9. android:textSize="17sp"/>
  10. <Spinner
  11. android:id="@+id/sp_icon"
  12. android:layout_width="match_parent"
  13. android:layout_height="wrap_content"
  14. android:spinnerMode="dropdown"/>
  15. </LinearLayout>

java代码

  1. public class SpinnerIconActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
  2. //定义下拉列表需要显示的图标数组
  3. private static final int[] iconArray = {
  4. R.drawable.csdn,R.drawable.csdn,R.drawable.csdn,R.drawable.csdn
  5. };
  6. //定义下拉列表需要显示的名称数组
  7. private static final String[] starArray = {"CSDN","CSDN","CSDN","CSDN"};
  8. @Override
  9. protected void onCreate(Bundle savedInstanceState) {
  10. super.onCreate(savedInstanceState);
  11. setContentView(R.layout.activity_spinner_icon);
  12. // 声明一个映射对象的列表,用于保存图标与名称配对信息
  13. List<Map<String,Object>> list = new ArrayList<>();
  14. // iconArray是图标数组,starArray是名称数组
  15. for (int i = 0; i < iconArray.length; i++) {
  16. Map<String,Object> item = new HashMap<>();
  17. item.put("icon",iconArray[i]);
  18. item.put("name",starArray[i]);
  19. list.add(item);
  20. }
  21. // 声明下拉列表的简单适配器,其中指定图标与文本两组数据
  22. SimpleAdapter startAdapter = new SimpleAdapter(this,list,R.layout.item_simple,
  23. new String[]{"icon","name"},
  24. new int[]{R.id.iv_icon,R.id.tv_name});
  25. Spinner sp_icon = findViewById(R.id.sp_icon);
  26. sp_icon.setAdapter(startAdapter);
  27. sp_icon.setSelection(0);
  28. sp_icon.setOnItemSelectedListener(this);
  29. }
  30. @Override
  31. public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
  32. Toast.makeText(this,"你选择的是"+starArray[i],Toast.LENGTH_SHORT);
  33. }
  34. @Override
  35. public void onNothingSelected(AdapterView<?> adapterView) {
  36. }
  37. }

layout下新建item_icon.xml文件

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:orientation="horizontal"
  3. android:layout_width="match_parent"
  4. android:layout_height="wrap_content">
  5. <ImageView
  6. android:id="@+id/iv_icon"
  7. android:layout_width="0dp"
  8. android:layout_weight="1"
  9. android:layout_height="50dp"
  10. android:src="@drawable/csdn"/>
  11. <TextView
  12. android:id="@+id/tv_name"
  13. android:layout_width="0dp"
  14. android:layout_weight="3"
  15. android:layout_height="match_parent"
  16. android:gravity="center"
  17. android:textColor="#ff0000"
  18. android:textSize="17sp"
  19. android:text="北京"/>
  20. </LinearLayout>

2、基本适配器BaseAdapter

BaseAdapter是一种适应性更强的基本适配器

例:

XML文件

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="match_parent"
  3. android:layout_height="match_parent"
  4. android:orientation="vertical">
  5. <TextView
  6. android:layout_width="match_parent"
  7. android:layout_height="wrap_content"
  8. android:text="基本适配器"
  9. android:textSize="17sp"/>
  10. <Spinner
  11. android:id="@+id/sp_base"
  12. android:layout_width="match_parent"
  13. android:layout_height="wrap_content"
  14. android:spinnerMode="dropdown"/>
  15. </LinearLayout>

java代码

  1. public class BaseAdapterActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
  2. private List<CSDN> list;
  3. @Override
  4. protected void onCreate(Bundle savedInstanceState) {
  5. super.onCreate(savedInstanceState);
  6. setContentView(R.layout.activity_base_adapter);
  7. Spinner sp_base = findViewById(R.id.sp_base);
  8. // 获取默认的列表
  9. list = CSDN.getDefaultList();
  10. // 构建适配器
  11. CSDNBaseAdapter adapter = new CSDNBaseAdapter(this, list);
  12. sp_base.setAdapter(adapter);
  13. sp_base.setSelection(0);
  14. sp_base.setOnItemSelectedListener(this);
  15. }
  16. @Override
  17. public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
  18. Toast.makeText(this,"你选择的是"+list.get(i).name,Toast.LENGTH_SHORT).show();
  19. }
  20. @Override
  21. public void onNothingSelected(AdapterView<?> adapterView) {
  22. }
  23. }

由于base适配器是抽象类不能直接使用,创建适配器类java代码

  1. public class CSDNBaseAdapter extends BaseAdapter {
  2. private Context mContext;
  3. private List<CSDN> mlist;
  4. public CSDNBaseAdapter(Context mContext, List<CSDN> list) {
  5. this.mContext = mContext;
  6. this.mlist = list;
  7. }
  8. @Override
  9. public int getCount() {
  10. return mlist.size();
  11. }
  12. @Override
  13. public Object getItem(int i) {
  14. return mlist.get(i);
  15. }
  16. @Override
  17. public long getItemId(int i) {
  18. return i;
  19. }
  20. @Override
  21. public View getView(int i, View view, ViewGroup viewGroup) {
  22. return null;
  23. }
  24. }

实体类

  1. public class CSDN {
  2. public int image;
  3. public String name;
  4. public String desc;
  5. public CSDN(int image,String name,String desc){
  6. this.image = image;
  7. this.name = name;
  8. this.desc = desc;
  9. }
  10. private static int[] iconArray = {
  11. R.drawable.csdn,R.drawable.csdn,R.drawable.csdn,R.drawable.csdn
  12. };
  13. private static String[] nameArray = {"CSDN","CSDN","CSDN","CSDN"};
  14. private static String[] descArray = {
  15. "shewyoo",
  16. "shewyoo",
  17. "shewyoo",
  18. "shewyoo"
  19. };
  20. public static List<CSDN> getDefaultList(){
  21. List<CSDN> list = new ArrayList<>();
  22. for (int i = 0; i < iconArray.length; i++) {
  23. list.add(new CSDN(iconArray[i],nameArray[i],descArray[i]));
  24. }
  25. return list;
  26. }
  27. }

layout下创建item_list.xml文件

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:orientation="horizontal"
  3. android:layout_width="match_parent"
  4. android:layout_height="wrap_content">
  5. <!-- 图像视图-->
  6. <ImageView
  7. android:id="@+id/iv_icon"
  8. android:layout_width="0dp"
  9. android:layout_weight="1"
  10. android:layout_height="80dp"
  11. android:scaleType="fitCenter"
  12. android:src="@drawable/csdn"/>
  13. <LinearLayout
  14. android:layout_width="0dp"
  15. android:layout_height="match_parent"
  16. android:layout_marginLeft="5dp"
  17. android:layout_weight="3"
  18. android:orientation="vertical">
  19. <!-- 文本视图-->
  20. <TextView
  21. android:id="@+id/tv_name"
  22. android:layout_width="match_parent"
  23. android:layout_height="0dp"
  24. android:layout_weight="1"
  25. android:gravity="start|center"
  26. android:textColor="@color/black"
  27. android:textSize="20sp"
  28. android:text="CSDN"/>
  29. <!-- 描述文本-->
  30. <TextView
  31. android:id="@+id/tv_desc"
  32. android:layout_width="match_parent"
  33. android:layout_height="0dp"
  34. android:layout_weight="2"
  35. android:gravity="start|center"
  36. android:textColor="@color/black"
  37. android:textSize="13sp"
  38. android:text="shewyoo"/>
  39. </LinearLayout>
  40. </LinearLayout>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/111514
推荐阅读
相关标签
  

闽ICP备14008679号