今天学习了Spinner组件,使用Spinner相当于从下拉列表中选择项目,下面演示一下Spinner的使用(分别使用ArrayAdapter和自定义Adapter实现)
(一):使用ArrayAdapter进行适配数据:
①:首先定义一个布局文件:
01 |
< span style = "font-size:16px;" ><? xml version = "1.0" encoding = "utf-8" ?> |
02 |
< LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android" |
03 |
android:layout_width = "fill_parent" |
04 |
android:layout_height = "fill_parent" |
05 |
android:orientation = "vertical" > |
08 |
android:id = "@+id/spinner1" |
09 |
android:layout_width = "match_parent" |
10 |
android:layout_height = "wrap_content" |
12 |
</ LinearLayout ></ span > |
【注意:】上面的Spinner有两个属性1:prompt是初始的时候,Spinner显示的数据,是一个引用类型
2:entries是直接在xml布局文件中绑定数据源(可以不设置,即可以在Activity中动态绑定)
②:建立数据源,使用数组,这些数据将会在Spinner下来列表中进行显示:
1 |
< span style = "font-size:16px;" ><? xml version = "1.0" encoding = "utf-8" ?> |
3 |
< string-array name = "spinnername" > |
③:接着在Activity中加入如下的代码(使用了系统定义的下拉列表的布局文件,当然也可以自定义)
2 |
mSpinner = (Spinner) findViewById(R.id.spinner1); |
4 |
String[] mItems = getResources().getStringArray(R.array.spinnername); |
6 |
ArrayAdapter<String> _Adapter= new ArrayAdapter<String>( this ,android.R.layout.simple_spinner_item, mItems); |
8 |
mSpinner.setAdapter(_Adapter); |
以上代码初步完成,看下运行效果:
下面是关于Spinner的点击事件(效果图如上图):
01 |
mSpinner.setOnItemSelectedListener( new OnItemSelectedListener() { |
03 |
public void onItemSelected(AdapterView<?> parent, View view, |
04 |
int position, long id) { |
05 |
String str=parent.getItemAtPosition(position).toString(); |
06 |
Toast.makeText(SpinnerActivity. this , "你点击的是:" +str, 2000 ).show(); |
09 |
public void onNothingSelected(AdapterView<?> parent) { |
(二)使用自定义的Adapter(重点)
①:定义每一个Item的布局文件
01 |
<? xml version = "1.0" encoding = "utf-8" ?> |
02 |
< LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android" |
03 |
android:layout_width = "match_parent" |
04 |
android:layout_height = "match_parent" |
05 |
android:orientation = "horizontal" > |
08 |
android:id = "@+id/textView1" |
09 |
android:layout_width = "wrap_content" |
10 |
android:layout_height = "wrap_content" |
11 |
android:drawableLeft = "@drawable/ic_launcher" |
12 |
android:paddingRight = "8dip" |
13 |
android:paddingTop = "8dip" |
14 |
android:text = "TextView" |
15 |
android:textSize = "25sp" /> |
18 |
android:id = "@+id/textView2" |
19 |
android:layout_width = "wrap_content" |
20 |
android:layout_height = "wrap_content" |
21 |
android:paddingLeft = "8dip" |
22 |
android:paddingTop = "8dip" |
23 |
android:text = "TextView" |
24 |
android:textSize = "25sp" /> |
②:建立Person类:
01 |
package com.jiangqq.csdn; |
03 |
private String personName; |
04 |
private String personAddress; |
05 |
public Person(String personName, String personAddress) { |
07 |
this .personName = personName; |
08 |
this .personAddress = personAddress; |
10 |
public String getPersonName() { |
13 |
public void setPersonName(String personName) { |
14 |
this .personName = personName; |
16 |
public String getPersonAddress() { |
19 |
public void setPersonAddress(String personAddress) { |
20 |
this .personAddress = personAddress; |
③:创建MyAdapter继承与BaseAdapter,进行适配:
01 |
package com.jiangqq.csdn; |
03 |
import java.util.List; |
04 |
import android.content.Context; |
05 |
import android.view.LayoutInflater; |
06 |
import android.view.View; |
07 |
import android.view.ViewGroup; |
08 |
import android.widget.BaseAdapter; |
09 |
import android.widget.TextView; |
13 |
* @author jiangqq <a href=http://blog.csdn.net/jiangqq781931404></a> |
16 |
public class MyAdapter extends BaseAdapter { |
17 |
private List<Person> mList; |
18 |
private Context mContext; |
20 |
public MyAdapter(Context pContext, List<Person> pList) { |
21 |
this .mContext = pContext; |
26 |
public int getCount() { |
31 |
public Object getItem( int position) { |
32 |
return mList.get(position); |
36 |
public long getItemId( int position) { |
43 |
public View getView( int position, View convertView, ViewGroup parent) { |
44 |
LayoutInflater _LayoutInflater=LayoutInflater.from(mContext); |
45 |
convertView=_LayoutInflater.inflate(R.layout.item, null ); |
48 |
TextView _TextView1=(TextView)convertView.findViewById(R.id.textView1); |
49 |
TextView _TextView2=(TextView)convertView.findViewById(R.id.textView2); |
50 |
_TextView1.setText(mList.get(position).getPersonName()); |
51 |
_TextView2.setText(mList.get(position).getPersonAddress()); |
④:在Activity中加入如下代码:
02 |
mSpinner = (Spinner) findViewById(R.id.spinner1); |
04 |
List<Person> persons= new ArrayList<Person>(); |
05 |
persons.add( new Person( "张三" , "上海 " )); |
06 |
persons.add( new Person( "李四" , "上海 " )); |
07 |
persons.add( new Person( "王五" , "北京" )); |
08 |
persons.add( new Person( "赵六" , "广州 " )); |
10 |
MyAdapter _MyAdapter= new MyAdapter( this , persons); |
12 |
mSpinner.setAdapter(_MyAdapter); |
运行效果如下截图:
监听事件和第一种方法相同:
Android中Spinner下拉列表(使用ArrayAdapter和自定义Adapter实现) .,,5-wow.com