【Android】-- 下拉列表Spinner、适配器Adapter

发布于:2022-12-02 ⋅ 阅读:(319) ⋅ 点赞:(0)

一、下拉列表 Spinner

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

例:下拉列表框

 XML文件

<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:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="下拉列表框"/>

    <Spinner
        android:id="@+id/sp_dropdown"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:spinnerMode="dropdown"/>
<!--    对话框模式将此改成dialog-->
        android:spinnerMode="dialog"/>


</LinearLayout>

 java代码

public class SpinnerDropdwnActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {

    private Spinner sp_dropdown;
//    定义下拉列表需要显示的文本数组
    private final static String[] starArray = {"天安门广场","天坛公园","故宫","北京动物园"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_spinner_dropdwn);
        sp_dropdown = findViewById(R.id.sp_dropdown);
//        声明数组适配器
        ArrayAdapter<String> startAdapter = new ArrayAdapter<>(this,R.layout.item_select,starArray);
//        设置下拉框标题,对话框模式才显示
        sp_dropdown.setPrompt("请选择地点");
        sp_dropdown.setAdapter(startAdapter);
//        设置默认显示第一项
        sp_dropdown.setSelection(0);
//        给下拉框设置选择监听器,一旦用户选中某一项,就触发监听器的onItemSelected方法
        sp_dropdown.setOnItemSelectedListener(this);

    }

    @Override
    public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
        Toast.makeText(this,"你选择的是"+starArray[position],Toast.LENGTH_SHORT).show();
    }

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

    }
}

layout下创建item_select.xml文件

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

二、适配器Adapter

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

 1、简单适配器SimpleAdapter

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

例:

 XML文件

<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:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="简单适配器"
        android:textSize="17sp"/>

    <Spinner
        android:id="@+id/sp_icon"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:spinnerMode="dropdown"/>
</LinearLayout>

java代码

public class SpinnerIconActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
//定义下拉列表需要显示的图标数组
    private static final int[] iconArray = {
            R.drawable.csdn,R.drawable.csdn,R.drawable.csdn,R.drawable.csdn
};
//定义下拉列表需要显示的名称数组
    private static final String[] starArray = {"CSDN","CSDN","CSDN","CSDN"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_spinner_icon);
//        声明一个映射对象的列表,用于保存图标与名称配对信息
        List<Map<String,Object>> list = new ArrayList<>();
//        iconArray是图标数组,starArray是名称数组
        for (int i = 0; i < iconArray.length; i++) {
            Map<String,Object> item = new HashMap<>();
            item.put("icon",iconArray[i]);
            item.put("name",starArray[i]);
            list.add(item);
        }
//        声明下拉列表的简单适配器,其中指定图标与文本两组数据
        SimpleAdapter startAdapter = new SimpleAdapter(this,list,R.layout.item_simple,
                new String[]{"icon","name"},
                new int[]{R.id.iv_icon,R.id.tv_name});
        Spinner sp_icon = findViewById(R.id.sp_icon);
        sp_icon.setAdapter(startAdapter);
        sp_icon.setSelection(0);
        sp_icon.setOnItemSelectedListener(this);
    }

    @Override
    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
        Toast.makeText(this,"你选择的是"+starArray[i],Toast.LENGTH_SHORT);
    }

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

    }
}

layout下新建item_icon.xml文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/iv_icon"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="50dp"
        android:src="@drawable/csdn"/>

    <TextView
        android:id="@+id/tv_name"
        android:layout_width="0dp"
        android:layout_weight="3"
        android:layout_height="match_parent"
        android:gravity="center"
        android:textColor="#ff0000"
        android:textSize="17sp"
        android:text="北京"/>

</LinearLayout>

2、基本适配器BaseAdapter

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

例:

XML文件

<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:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="基本适配器"
        android:textSize="17sp"/>

    <Spinner
        android:id="@+id/sp_base"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:spinnerMode="dropdown"/>
</LinearLayout>

java代码

public class BaseAdapterActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {

    private List<CSDN> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_base_adapter);
        Spinner sp_base = findViewById(R.id.sp_base);
//        获取默认的列表
        list = CSDN.getDefaultList();
//        构建适配器
        CSDNBaseAdapter adapter = new CSDNBaseAdapter(this, list);
        sp_base.setAdapter(adapter);
        sp_base.setSelection(0);
        sp_base.setOnItemSelectedListener(this);
    }

    @Override
    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
        Toast.makeText(this,"你选择的是"+list.get(i).name,Toast.LENGTH_SHORT).show();
    }

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

    }
}

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

public class CSDNBaseAdapter extends BaseAdapter {
    private Context mContext;
    private List<CSDN> mlist;

    public CSDNBaseAdapter(Context mContext, List<CSDN> list) {
        this.mContext = mContext;
        this.mlist = list;
    }

    @Override
    public int getCount() {
        return mlist.size();
    }

    @Override
    public Object getItem(int i) {
        return mlist.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        return null;
    }
}

实体类

public class CSDN {
    public int image;
    public String name;
    public String desc;

    public CSDN(int image,String name,String desc){
        this.image = image;
        this.name = name;
        this.desc = desc;
    }
    private static int[] iconArray = {
            R.drawable.csdn,R.drawable.csdn,R.drawable.csdn,R.drawable.csdn
    };
    private static String[] nameArray = {"CSDN","CSDN","CSDN","CSDN"};
    private static String[] descArray = {
            "shewyoo",
            "shewyoo",
            "shewyoo",
            "shewyoo"
    };
    public static List<CSDN> getDefaultList(){
        List<CSDN> list = new ArrayList<>();
        for (int i = 0; i < iconArray.length; i++) {
            list.add(new CSDN(iconArray[i],nameArray[i],descArray[i]));
        }
        return list;
    }
}

layout下创建item_list.xml文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

<!--    图像视图-->
    <ImageView
        android:id="@+id/iv_icon"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="80dp"
        android:scaleType="fitCenter"
        android:src="@drawable/csdn"/>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_marginLeft="5dp"
        android:layout_weight="3"
        android:orientation="vertical">

<!--        文本视图-->
        <TextView
            android:id="@+id/tv_name"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:gravity="start|center"
            android:textColor="@color/black"
            android:textSize="20sp"
            android:text="CSDN"/>
<!--        描述文本-->
        <TextView
            android:id="@+id/tv_desc"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="2"
            android:gravity="start|center"
            android:textColor="@color/black"
            android:textSize="13sp"
            android:text="shewyoo"/>
    </LinearLayout>
</LinearLayout>

本文含有隐藏内容,请 开通VIP 后查看