android Spinner的使用
首先是MainActivity
package com.example.spinnertest; import java.util.ArrayList; import java.util.List; import android.os.Bundle; import android.app.Activity; import android.content.Context; import android.text.TextUtils; import android.view.LayoutInflater; import android.view.Menu; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.AdapterView.OnItemSelectedListener; import android.widget.ArrayAdapter; import android.widget.Spinner; import android.widget.TextView; import com.example.spinnertest.R; public class MainActivity extends Activity { private Spinner myS; private List<String> datas=new ArrayList<String>(); private MySpinnerAdapter sAdapter; private String value; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); myS = (Spinner)findViewById(R.id.spinnerS); initData(); sAdapter = new MySpinnerAdapter(this,datas); myS.setAdapter(sAdapter); refreshShowView(); myS.setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub value = sAdapter.getItem(arg2); } @Override public void onNothingSelected(AdapterView<?> arg0) { // TODO Auto-generated method stub } }); } private void initData() { // TODO Auto-generated method stub datas.add("a"); datas.add("b"); datas.add("c"); datas.add("d"); datas.add("e"); datas.add("f"); value = datas.get(4); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } class MySpinnerAdapter extends ArrayAdapter<String> { private List<String> mList; private Context mContext; class ViewHolder { public TextView spinnerTv; public ViewHolder(View v){ spinnerTv = (TextView)v.findViewById(R.id.spinner_tv); } } public MySpinnerAdapter(Context context, List<String> list) { super(context,R.layout.spinner_item,R.id.spinner_tv, list); // TODO Auto-generated constructor stub setData(list); mContext = context; } public void setData(List<String> list){ mList = list; notifyDataSetChanged(); } @Override public int getCount() { // TODO Auto-generated method stub if(mList==null){ return 0; } return mList.size(); } @Override public String getItem(int position) { // TODO Auto-generated method stub if(mList==null){ return null; } return mList.get(position); } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @Override public View getDropDownView(int position, View convertView, ViewGroup parent) { ViewHolder holder = null; if(convertView==null){ convertView = LayoutInflater.from(mContext).inflate( R.layout.spinner_item, null); holder= new ViewHolder(convertView); convertView.setTag(holder); }else{ holder = (ViewHolder)convertView.getTag(); } String myListItem = mList.get(position); holder.spinnerTv.setText(myListItem); return convertView; } } private void refreshShowView(){ if(!TextUtils.isEmpty(value)){ int i = 0; for(String item:datas){ if(value.equals(item)){ break; } i++; } if(i==datas.size()){ i = datas.size()-1; } myS.setSelection(i); } }; }
然后是activity_main.xml
<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="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:orientation="horizontal" android:gravity="center_vertical" tools:context=".MainActivity" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="标题"/> <Spinner android:id="@+id/spinnerS" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content"/> </LinearLayout>
然后是spinner_item:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:paddingTop="10dp" android:paddingBottom="10dp" android:id="@+id/spinner_tv"/> </RelativeLayout>