目录1.应用场景2.常用属性3.xml数据源布局实现Spinner4.ArrayadapterArrayList布局实现Spinner5.自定义adapter数据源布局实现Spin 目录 1.应用场景 2.常用属性 3.xml数据源布局实现Spin
目录1.应用场景2.常用属性3.xml数据源布局实现Spinner4.ArrayadapterArrayList布局实现Spinner5.自定义adapter数据源布局实现Spin 目录 1.应用场景 2.常用属性 3.xml数据源布局实现Spinner 4.ArrayadapterArrayList布局实现Spinner 5.自定义adapter数据源布局实现Spinner 6.多级连联动Spinner实现(市县区三级联动) 1.应用场景 Spinner即列表选项框多应用于城市选择或者一些搜索分类选取一种的情况 2.常用属性
android:entries"values/"android:popupBackground""android:dropDownSelector""android:layout_width"wrap_content"android:layout_height"wrap_content"android:dropDownHorizontalOffset"12dp"android:dropDownVerticalOffset"1dp"android:dropDownWidth"40dp"android:spinnerMode"dropdown|dialog"android:scrollbars"vertical"android:prompt""android:scrollbarThumbHorizontal"mipmap/ic_launcher" android:scrollbarTrackHorizontal"" 3.xml数据源布局实现Spinner 1)数据源myarraylist.xml
上海北京广州深圳杭州成都 2)主布局xml
3)Java类文件
package com.example.spinnerui;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.util.Log;import android.view.View;import android.widget.AdapterView;import android.widget.Spinner;import android.widget.Toast;import java.util.ArrayList;public class MainActivity extends AppCompatActivity {private Spinner dropdownSp;//Spinner默认选择第一项所以刚进入是会触发onItemSelected事件为了解决这个问题定义类firstSelectprivate boolean firstSelecttrue;Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//初始化dropdownSp (Spinner) findViewById(R.id.dropdownSp);//设置监听dropdownSp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {/** parent:指示spinner* view显示item的空间这里指TextView* position被选中的item的位置* id选中项的id* */Overridepublic void onItemSelected(AdapterView parent, View view, int position, long id) {if (!firstSelect){//打印提示parent.getItemAtPosition(position)获取选中位置的值Toast.makeText(MainActivity.this,"你选择了"parent.getItemAtPosition(position).toString(),Toast.LENGTH_SHORT).show();Log.d("id",id"");Log.d("position",position"");Log.d("view",view.toString());Log.d("parent",parent.toString());}else {firstSelectfalse;}}Overridepublic void onNothingSelected(AdapterView parent) {}});}} 4)效果截图
4.ArrayadapterArrayList布局实现Spinner 1)自定义的已选取item的布局drop_item.xml
2)主布局文件
3)Java文件代码
package com.example.spinnerui;import android.content.Context;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.util.Log;import android.view.View;import android.widget.AdapterView;import android.widget.ArrayAdapter;import android.widget.Spinner;import android.widget.Toast;import java.util.ArrayList;public class SecondActivity extends AppCompatActivity {private Spinner dialogSp;private boolean firstSelecttrue;Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_second);//初始化dialogSp (Spinner) findViewById(R.id.dialogSp);//初始化数据源ArrayList arraynew ArrayList();for (int i0;i<5;i){array.add(i,"数据"i);}/** ArrayAdapter(Context context, int resource, List objects)* context表示上下文* int resource选取的item的布局* objects数据* *///系统内置的选取的Item的TextView布局android.R.layout.simple_spinner_item//自定义的选取的Item的TextView布局R.layout.drop_itemArrayAdapter myArrayAdapter new ArrayAdapter(this,R.layout.drop_item,array);//设置列表框item的布局myArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);//设置适配器dialogSp.setAdapter(myArrayAdapter);//设置监听dialogSp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {/** parent:指示spinner* view显示item的空间这里指TextView* position被选中的item的位置* id选中项的id* */Overridepublic void onItemSelected(AdapterView parent, View view, int position, long id) {if (!firstSelect){//打印提示parent.getItemAtPosition(position)获取选中位置的值Toast.makeText(SecondActivity.this,"你选择了"parent.getItemAtPosition(position).toString(),Toast.LENGTH_SHORT).show();Log.d("id",id"");Log.d("position",position"");Log.d("view",view.toString());Log.d("parent",parent.toString());}else {firstSelectfalse;}}Overridepublic void onNothingSelected(AdapterView parent) {}});}} 4)从xml文件加载数据适配
ArrayAdapter myArrayAdapter ArrayAdapter.createFromResource(this, R.array.myarraydata,android.R.layout.simple_spinner_item); 5)效果截图 5.自定义adapter数据源布局实现Spinner 1)自定义adapterMyCustomAdapter.java
package com.example.spinnerui;import android.content.Context;import android.graphics.drawable.Drawable;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.ImageView;import android.widget.TextView;import java.util.List;import java.util.Map;import java.util.Objects;/*** Created by elimy on 2016-08-10.*/public class MyCustomAdapter extends BaseAdapter {private Context context;private List