Spinner
ArrayAdapter
主布局、适配器、item布局与内容
- 主布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".spinnerDropDownActivity"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center"
android:text="下拉选项" />
<Spinner
android:id="@+id/spinner_id"
android:layout_width="match_parent"
android:layout_height="50dp"
android:spinnerMode="dropdown" />
</LinearLayout>
适配器
public class spinnerDropDownActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
// 数据
List<String> names = Arrays.asList(new String[]{"火星", "地球","金星","水星"});
private Spinner spinnerId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_spinner_drop_down);
initView();
// 适配器
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, R.layout.item_view, names);
spinnerId.setAdapter(adapter);
spinnerId.setSelection(0);
spinnerId.setOnItemSelectedListener(this);
}
private void initView() {
spinnerId = (Spinner) findViewById(R.id.spinner_id);
}
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
Log.e(TAG, "onItemSelected: int " + i + " long:" + l);
Toast.makeText(this,names.get(i),Toast.LENGTH_LONG).show();
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
}
item布局
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center"
android:textColor="@color/black"
android:textSize="17dp"
tools:text="火星">
</TextView>
/**
* Constructor
*
* @param context The current context.
* @param resource The resource ID for a layout file containing a TextView to use when
* instantiating views.
* @param objects The objects to represent in the ListView.
*/
public ArrayAdapter(@NonNull Context context, @LayoutRes int resource,
@NonNull List<T> objects) {
this(context, resource, 0, objects);
}
SimpleAdapter
simpleAdapter与ArrayAdapter类似,不同之处在于传入参数 不同:
/**
* Constructor
*
* @param context The context where the View associated with this SimpleAdapter is running
* @param data A List of Maps. Each entry in the List corresponds to one row in the list. The
* Maps contain the data for each row, and should include all the entries specified in
* "from"
* @param resource Resource identifier of a view layout that defines the views for this list
* item. The layout file should include at least those named views defined in "to"
* @param from A list of column names that will be added to the Map associated with each
* item.
* @param to The views that should display column in the "from" parameter. These should all be
* TextViews. The first N views in this list are given the values of the first N columns
* in the from parameter.
*/
public SimpleAdapter(Context context, List<? extends Map<String, ?>> data,
@LayoutRes int resource, String[] from, @IdRes int[] to) {
mData = data;
mResource = mDropDownResource = resource;
mFrom = from;
mTo = to;
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
package com.example.viewpager;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.SimpleAdapter;
import android.widget.Spinner;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MySimpleAdapter extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
private Spinner spinnerId;
// 定义下拉列表需要显示的行星图标数组
private static final int[] iconArray = {
R.drawable.shuixing, R.drawable.jinxing, R.drawable.diqiu,
R.drawable.huoxing, R.drawable.muxing, R.drawable.tuxing
};
// 定义下拉列表需要显示的行星名称数组
private static final String[] starArray = {"水星", "金星", "地球", "火星", "木星", "土星"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_simple_adapater);
initView();
List<Map<String, Object>> list = new ArrayList<>();
for (int i = 0; i < iconArray.length; i++) {
HashMap<String, Object> map = new HashMap<>();
map.put("icon", iconArray[i]);
map.put("name", starArray[i]);
list.add(map);
}
// 这是simpleAdapter 的传入参数 from指的是数据 to指的是视图对应id
SimpleAdapter simpleAdapter = new SimpleAdapter(this, list,
R.layout.simple_item,
new String[]{"icon", "name"},
new int[]{R.id.icon, R.id.tv_id});
spinnerId.setAdapter(simpleAdapter);
spinnerId.setPrompt("请选择:");
spinnerId.setSelection(0);
spinnerId.setOnItemSelectedListener(this);
}
private void initView() {
spinnerId = (Spinner) findViewById(R.id.spinner_id);
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(this, "选择了 " + starArray[position], Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
<?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="wrap_content"
android:gravity="center"
android:orientation="horizontal"
android:textColor="@color/black">
<ImageView
android:id="@+id/icon"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:src="@drawable/diqiu" />
<TextView
android:id="@+id/tv_id"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"
android:gravity="center"
android:text="地球"
android:textSize="17sp" />
</LinearLayout>
BaseAdapter
四个要素:主布局、实现的BaseAdapter、数据类以及数据、item布局、主程序
- 主布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center"
android:text="下拉选项" />
<Spinner
android:id="@+id/spinner_id"
android:layout_width="match_parent"
android:layout_height="50dp"
android:spinnerMode="dropdown" />
</LinearLayout>
- 实现的BaseAdapter:
public class PlanetBaseAdapter extends BaseAdapter {
private Context context;
private List<Planet> planetList;
public PlanetBaseAdapter(Context context,List<Planet> planetList){
this.context = context;
this.planetList = planetList;
}
// count返回的是数据的总数
@Override
public int getCount() {
return planetList.size();
}
// 返回的是对应位置的数据
@Override
public Object getItem(int position) {
return planetList.get(position);
}
// 返回的是item对应的ID,可以设置为position
@Override
public long getItemId(int position) {
return position;
}
// 返回的是item页面的View
@Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = LayoutInflater.from(context).inflate(R.layout.item_layout, null);
ImageView imageView = (ImageView) convertView.findViewById(R.id.icon);
TextView tv_desc = (TextView) convertView.findViewById(R.id.tv_desc);
TextView tv_name = (TextView) convertView.findViewById(R.id.tv_name);
Planet planet = planetList.get(position);
imageView.setImageResource(planet.image);
tv_name.setText(planet.name);
tv_desc.setText(planet.desc);
return convertView;
}
}
- 数据类与数据:
public class Planet {
public int image; // 行星图标
public String name; // 行星名称
public String desc; // 行星描述
public Planet(int image, String name, String desc) {
this.image = image;
this.name = name;
this.desc = desc;
}
private static int[] iconArray = {R.drawable.shuixing, R.drawable.jinxing, R.drawable.diqiu,
R.drawable.huoxing, R.drawable.muxing, R.drawable.tuxing};
private static String[] nameArray = {"水星", "金星", "地球", "火星", "木星", "土星"};
private static String[] descArray = {
"水星是太阳系八大行星最内侧也是最小的一颗行星,也是离太阳最近的行星",
"金星是太阳系八大行星之一,排行第二,距离太阳0.725天文单位",
"地球是太阳系八大行星之一,排行第三,也是太阳系中直径、质量和密度最大的类地行星,距离太阳1.5亿公里",
"火星是太阳系八大行星之一,排行第四,属于类地行星,直径约为地球的53%",
"木星是太阳系八大行星中体积最大、自转最快的行星,排行第五。它的质量为太阳的千分之一,但为太阳系中其它七大行星质量总和的2.5倍",
"土星为太阳系八大行星之一,排行第六,体积仅次于木星"
};
public static List<Planet> getDefaultList() {
List<Planet> planetList = new ArrayList<Planet>();
for (int i = 0; i < iconArray.length; i++) {
planetList.add(new Planet(iconArray[i], nameArray[i], descArray[i]));
}
return planetList;
}
}
- item布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"
android:textColor="@color/black">
<ImageView
android:id="@+id/icon"
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_weight="1"
android:src="@drawable/diqiu" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="3"
tools:ignore="Suspicious0dp">
<TextView
android:id="@+id/tv_name"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center"
android:text="地球" />
<TextView
android:id="@+id/tv_desc"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:gravity="center"
android:text="地球" />
</LinearLayout>
</LinearLayout>
- 主程序
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
private Spinner spinnerId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
PlanetBaseAdapter planetBaseAdapter = new PlanetBaseAdapter(this, Planet.getDefaultList());
spinnerId.setAdapter(planetBaseAdapter);
spinnerId.setSelection(0);
spinnerId.setOnItemSelectedListener(this);
}
private void initView() {
spinnerId = (Spinner) findViewById(R.id.spinner_id);
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(this,"你选择了:" + Planet.getDefaultList().get(position).name,Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
- 实现的BaseAdapter可优化:
public class PlanetBaseAdapter extends BaseAdapter {
private Context context;
private List<Planet> planetList;
public PlanetBaseAdapter(Context context,List<Planet> planetList){
this.context = context;
this.planetList = planetList;
}
@Override
public int getCount() {
return planetList.size();
}
@Override
public Object getItem(int position) {
return planetList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null){
holder = new ViewHolder();
//获取当前界面的布局
convertView = LayoutInflater.from(context).inflate(R.layout.item_layout, null);
holder.image = (ImageView) convertView.findViewById(R.id.icon);
holder.name = (TextView) convertView.findViewById(R.id.tv_name);
holder.desc = (TextView) convertView.findViewById(R.id.tv_desc);
convertView.setTag(holder);
}else {
holder = (ViewHolder)convertView.getTag();
}
// 给控制设置好数据
Planet planet = planetList.get(position);
holder.image.setImageResource(planet.image);
holder.name.setText(planet.name);
holder.desc.setText(planet.desc);
return convertView;
}
private class ViewHolder{
public ImageView image;
public TextView name;
public TextView desc;
}
}
- 理解:
- getView需要返回一个已经赋值的View convertView;
- 创建了一个类ViewHolder指向convertView中所有需要赋值的属性;
- 将ViewHolder放在convertView的Tag中从而与convertView绑定。(此时convertView中需要赋值的属性已经与其Tag中的ViewHolder的属性绑定);
- 对ViewHolder 进行赋值(就相当于对convertView中所有需要赋值的属性进行赋值)。
文本视图
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background = "@color/white"
android:orientation="horizontal">
<TextView
android:id="@+id/tv_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="1"
android:text="提示语"
android:textColor="@color/black"
android:textSize="20sp"/>
<EditText
android:id="@+id/tv_desc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="1"
android:hint="输入内容"
android:textSize="20sp"
android:textColor="@color/black"/>
</LinearLayout>