The latest spinner usage detailed
Spinner
A total of two components, one is its own
Spinner
, one is
android.support.v7.widget.AppCompatSpinner
The difference between the two is
v7
Within the
Spinner
is compatible with the lower version,
Spinner
A later version of the method can be used in exchange for
v7
Under the
Spinner
Can always be compatible to
2.1
(
v7
Compatible to
api7
), there is no other difference between the use of the first and the other, it is recommended
v7
To ensure that the effects are displayed on different versions.
Spinner
Steps to use:
The layout of the data source and theme, characterized by convenient and fast, the disadvantage is not to dynamically change the data to be displayed, the use of adapter features are flexible, for the project needs to choose.
xml
Set up data sources and themes in data sources
//使用entries属性,传入的是values文件夹下的arrays.xml内的数据 <android.support.v7.widget.AppCompatSpinner android:layout_width="match_parent" android:layout_height="wrap_content" android:entries="@array/week">
Values/arrays.xml file
<?xml version= "1.0" encoding= "Utf-8"?><resources> <string-array name="Week"> <item >Monday</Item> <item >Tuesday</Item> <item >Wednesday</Item> <item >Thursday</Item> <item >Friday</Item> <item >Saturday</Item> <item >Sunday</Item> </string-array></Resources>
After the above operation
Spinner
can be used (after clicking the event and then adding), the effect is as follows: Here is the common XML attributes to paste:
Are better understood, explain several of these properties:
android:spinnerMode //显示模式 :popu和dialog两种//当显示模式为dialog时生效,作用为显示dialog的标题内容
Show settings for a theme
app:theme="@style/myDropTheme" //设置的是下拉item的主题 ,但是会覆盖未下拉的显示//所以如果不想再代码中设置主题的话最好主题设置成白色背景的,效果比较好
Style.xml-mydroptheme
name="myDropTheme" parent="Widget.AppCompat.DropDownItem.Spinner"> <itemname="android:textColor">@color/colorAccent</item> <itemname="android:textSize">20sp</item> <itemname="android:background">@color/colorPrimary</item> </style>
As follows:
To add a response event:
Spinner
The response event is
OnItemSelectedListener
, never write
onItemClickListener
, or direct error:
mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override publicvoidonItemSelectedintlong id) { //选择列表项的操作 } @Override publicvoidonNothingSelected(AdapterView<?> parent) { //未选中时候的操作 } });
Set in code
Spinner
The layout file only needs to contain basic high-width properties to choose the adapter: The most convenient adapter is
ArrayAdapter
, the disadvantage is that you can only display a single
TextView
Content (with
List
Store data)
ArrayAdapter arrayAdapter = new ArrayAdapter(mContext, R.layout.item_select, mList); //传入的参数分别为 Context , 未选中项的textview , 数据源List//单独设置下拉的textviewarrayAdapter.setDropDownViewResource(R.layout.item_drop);
R.layout.item_select
<?xml version= "1.0" encoding= "Utf-8"?><TextView xmlns:android="Http://schemas.android.com/apk/res/android" Android:layout_width="Match_parent" android:textsize="20DP" Android:typeface="Serif" android:gravity="Center" Android:textcolor="#000000" Android:layout_height="Match_parent" Android:background="@color/coloraccent" android:orientation="Vertical"></TextView>
R.layout.item_drop
<?xml version= "1.0" encoding= "Utf-8"?><TextView xmlns:android="Http://schemas.android.com/apk/res/android" Android:layout_width="Match_parent" Android:layout_height="Match_parent" android:padding="5DP" Android:background="@color/colorprimary" android:gravity="Center" android:orientation="Vertical" Android:textcolor="#000000" android:textsize="20DP"></TextView>
The effect is as follows (the color is set to differentiate:
Custom
Adapter
The actual requirements may require
Spinner
Drop-down does not just show a
TextView
, you will need to customize the adapter at this time
Private class myadapter implements spinneradapter { PrivateThemedspinneradapter.helper Helper;@Override Public void Registerdatasetobserver(Datasetobserver observer) { }@Override Public void Unregisterdatasetobserver(Datasetobserver observer) { }@Override Public int GetCount() {returnMlist.size (); }@Override PublicObjectGetItem(intPosition) {returnMlist.get (position); }@Override Public Long Getitemid(intPosition) {returnPosition }@Override Public Boolean Hasstableids() {return false; }@Override PublicViewGetView(intPosition, View Convertview, ViewGroup parent) {TextView TextView =NewTextView (Mcontext); Textview.settextsize ( -); Textview.setbackgroundcolor (Getresources (). GetColor (r.color.coloraccent)); Textview.settext (Mlist.get (position));returnTextView; }@Override Public int Getitemviewtype(intPosition) {return 1; }@Override Public int Getviewtypecount() {return 1; }@Override Public Boolean IsEmpty() {return false; }@Override PublicViewGetdropdownview(intPosition, View Convertview, ViewGroup parent) {TextView TextView =NewTextView (Mcontext); Textview.settextsize ( -); Textview.setbackgroundcolor (Getresources (). GetColor (r.color.colorprimary)); Textview.settext (Mlist.get (position));returnTextView; } }
The effect is as follows (the color is set for the drop-down and default display as a distinction, and a drop-down offset is not used for ease of referencing the layout file):
At this point the use of the adapter is completed, you can achieve the default display and drop-down using the same layout, but also different layouts, basically meet the requirements of the other there are several
23
In the new method, you can do more customization (use Appcomptspinner to fit into the lower version):
mSpinner.setPopupBackgroundResource(R.drawable.back);mSpinner.setBackgroundResource(R.drawable.back);//给默认显示项和下拉菜单设置背景
Simple Add a background
Complete
The latest spinner usage detailed