admin管理员组

文章数量:1122847

android spinner 简书,控件

一、概念

Spinner是下拉列表控件。

二、使用

1.属性

android:entries 使用该属性就可以不用设置Spinner的Adapter,也可以填充数据。

android:spinnerMode 可选值有:"dialog"(对话框形式)、"dropdown"(下拉框形式)。

2.点击事件

city.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

@Override

public void onItemSelected(AdapterView> adapterView, View view, int i, long l) {

//这个方法里可以对点击事件进行处理

//i指的是点击的位置,通过i可以取到相应的数据源

}

@Override

public void onNothingSelected(AdapterView> adapterView) {

}

});

用代码来点击spinner:

//选择Spinner里的第二个数据

city.setSelection(1,true);

3.添加数据

使用xml文件作为数据源:

private void initView(){

city= (Spinner) findViewById(R.id.spCity);

SpinnerAdapter adapter=null;

adapter=ArrayAdapter.createFromResource(this,R.array.cities,android.R.layout.simple_spinner_dropdown_item);

city.setAdapter(adapter);

}

使用数组或者是List作为数据数据源:

ArrayList list=new ArrayList<>();

SpinnerAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_spinner_item,list);

4.自定义Spinner

改变初始布局:

//strings.xml

一年级

二年级

三年级

四年级

五年级

六年级

//spinner_layout.xml

android:layout_width = "wrap_content"

android:layout_height = "wrap_content"

xmlns:tools = ""

android:textColor = "#6d6d6d"

android:textSize = "15sp"

android:drawableRight = "@drawable/arrow"

android:drawablePadding = "5dp"

tools:text = "一年级" >

//ArrayAdapter

ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.grade, R.layout.spinner_layout);

//Activity

spinner.setBackgroundColor(0x0);

改变列表item布局:

ArrayAdapter的构造函数有mResource和mDropDownResource两个变量,其中mResource是初始布局,mDropDownResource是列表item的布局。在createFromResource方法中,mResource和mDropDownResource赋值相同。另外ArrayAdapter还有一个setDropDownViewResource方法可以改变列表item布局。

//spinner_item.xml

android:layout_width="match_parent"

android:layout_height="wrap_content"

xmlns:tools=""

android:textColor="#6d6d6d"

android:textSize="15sp"

android:padding="8dp"

android:gravity="center_horizontal"

tools:text="一年级">

//Activity

adapter.setDropDownViewResource(R.layout.spinner_item);

改变弹窗背景及位置:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {

spinner.setPopupBackgroundResource(R.drawable.bg_spinner);

spinner.setDropDownVerticalOffset(dip2px(20));

}

添加选中效果:

//SpinnerAdapter

public class SpinnerAdapter extends ArrayAdapter {

private int selectedPostion;

public void setSelectedPostion(int selectedPostion) {

this.selectedPostion = selectedPostion;

}

public SpinnerAdapter(@NonNull Context context, int resource, @NonNull T[] objects) {

super(context, resource, objects);

}

@Override

public View getDropDownView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

View view = super.getDropDownView(position, convertView, parent);

TextView textView = (TextView) view;

if (selectedPostion == position) {

textView.setTextColor(0xff373741);

textView.getPaint().setFakeBoldText(true);

} else {

textView.setTextColor(0xff6d6d6d);

textView.getPaint().setFakeBoldText(false);

}

return view;

}

public static @NonNull

SpinnerAdapter createFromResource(@NonNull Context context, @ArrayRes int textArrayResId, @LayoutRes int textViewResId) {

final CharSequence[] strings = context.getResources().getTextArray(textArrayResId);

return new SpinnerAdapter<>(context, textViewResId, strings);

}

}

//Activity

Spinner spinner = (Spinner) findViewById(R.id.spinner);

adapter = SpinnerAdapter.createFromResource(this,R.array.grade,R.layout.spinner_layout);

adapter.setDropDownViewResource(R.layout.spinner_item);

spinner.setBackgroundColor(0x0);

if(Build.VERSION.SDK_INT >=Build.VERSION_CODES.JELLY_BEAN){

spinner.setPopupBackgroundResource(R.drawable.bg_spinner);

spinner.setDropDownVerticalOffset(dip2px(20));

}

spinner.setAdapter(adapter);

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){

@Override

public void onItemSelected (AdapterView < ? > parent, View view,int position,long id){

adapter.setSelectedPostion(position);

}

@Override

public void onNothingSelected (AdapterView < ? > parent){

}

});

本文标签: android spinner 简书控件