关于Spinner的解释及基本用法,已经可以找到很多例子:
Spinner中文API:http://www.cnblogs.com/over140/archive/2010/11/17/1879794.html
用法:http://blog.csdn.net/dadahacker/archive/2010/07/17/5741865.aspx
http://yilee.info/android-spinner.html
在这里,我想向大家介绍一种高级的用法,在下拉列表的菜单项中显示相应的Icon,解决问题的关键就是需要重写Adapter,关于Adapter呢,大家可以参考http://yahaitt.javaeye.com/blog/453060
这不是本文的重点,本范例主要通过重写Adapter,。
一般我们需要重写Adapter类的四个方法即可,分别是public int getCount() 、public Object getItem(int position)、public long getItemId(int position)
和public View getView(int position, View convertView, ViewGroup parent)。
新建一个IconSpinnerAdapter类,继承BaseAdapter,实现上面四个必须覆盖的方法:
- public class IconSpinnerAdapter extends BaseAdapter {
- private Context mContext;
- private int mDropDownResource;
- private boolean mNotifyOnChange = true;
- private List<BabyInfo> mBabyInfoList;
- private LayoutInflater mInflater;
- private int mResource;
- private int mTextViewResourceId;
- private int mImageViewResourceId;
- private ArrayList<BabyInfo> mOriginalValues;
- private Object mLock;
- @Override
- public int getCount() {
- return mBabyInfoList.size();
- }
- @Override
- public Object getItem(int position) {
- return mBabyInfoList.get(position);
- }
- @Override
- public long getItemId(int position) {
- return mBabyInfoList.get(position).getBabyID();
- }
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- return createViewFromResource(position, convertView, parent, mResource);
- }
- private View createViewFromResource(int position, View convertView,
- ViewGroup parent, int resource) {
- View view;
- ImageView imageView;
- TextView text;
- if (convertView == null) {
- view = mInflater.inflate(resource, parent, false);
- } else {
- view = convertView;
- }
- BabyInfo babyInfo = (BabyInfo) getItem(position);
- imageView = (ImageView) view.findViewById(mImageViewResourceId);
- imageView.setAdjustViewBounds(true);
- imageView.setImageResource(babyInfo.getImgResource());
- text = (TextView) view.findViewById(mTextViewResourceId);
- text.setText(babyInfo.getBabyName());
- return view;
- }
- public IconSpinnerAdapter(Context context, int resource,
- int imageViewResourceId, int textViewResourceId) {
- mContext = context;
- mInflater = (LayoutInflater) context
- .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
- mResource = mDropDownResource = resource;
- mBabyInfoList = new ArrayList<BabyInfo>();
- mImageViewResourceId = imageViewResourceId;
- mTextViewResourceId = textViewResourceId;
- }
- public IconSpinnerAdapter(Context context, int resource,
- int imageViewResourceId, int textViewResourceId,
- List<BabyInfo> babyInfoList) {
- mContext = context;
- mInflater = (LayoutInflater) context
- .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
- mResource = mDropDownResource = resource;
- mBabyInfoList = babyInfoList;
- mImageViewResourceId = imageViewResourceId;
- mTextViewResourceId = textViewResourceId;
- }
- /**
- * Sets the layout resource to create the drop down views.
- *
- * @param resource
- * the layout resource defining the drop down views
- */
- public void setDropDownViewResource(int resource) {
- mDropDownResource = resource;
- }
- @Override
- public View getDropDownView(int position, View convertView, ViewGroup parent) {
- return createViewFromResource(position, convertView, parent,
- mDropDownResource);
- }
当然,为了使功能更加强大,可以重写其他方法:
使用到一个自定义的BabyInfo类,懒得修改,直接拿过来用了,你可以封装其他对象:
- /**
- * Adds the specified object at the end of the array.
- *
- * @param babyInfo
- * The baby info to add at the end of the array.
- */
- public void add(BabyInfo babyInfo) {
- if (mOriginalValues != null) {
- synchronized (mLock) {
- mOriginalValues.add(babyInfo);
- if (mNotifyOnChange)
- notifyDataSetChanged();
- }
- } else {
- mBabyInfoList.add(babyInfo);
- if (mNotifyOnChange)
- notifyDataSetChanged();
- }
- }
- /**
- *
- * @param babyInfo
- * The baby info to insert into the array.
- * @param index
- * The index at which the object must be inserted.
- */
- public void insert(BabyInfo babyInfo, int index) {
- if (mOriginalValues != null) {
- synchronized (mLock) {
- mOriginalValues.add(index, babyInfo);
- if (mNotifyOnChange)
- notifyDataSetChanged();
- }
- } else {
- mBabyInfoList.add(index, babyInfo);
- if (mNotifyOnChange)
- notifyDataSetChanged();
- }
- }
- /**
- * Removes the specified object from the array.
- *
- * @param babyInfo
- * The baby info to remove.
- */
- public void remove(BabyInfo babyInfo) {
- if (mOriginalValues != null) {
- synchronized (mLock) {
- mOriginalValues.remove(babyInfo);
- }
- } else {
- mBabyInfoList.remove(babyInfo);
- }
- if (mNotifyOnChange)
- notifyDataSetChanged();
- }
- /**
- * Remove all elements from the list.
- */
- public void clear() {
- if (mOriginalValues != null) {
- synchronized (mLock) {
- mOriginalValues.clear();
- }
- } else {
- mBabyInfoList.clear();
- }
- if (mNotifyOnChange)
- notifyDataSetChanged();
- }
- /**
- * Sorts the content of this adapter using the specified comparator.
- *
- * @param comparator
- * The comparator used to sort the objects contained in this
- * adapter.
- */
- public void sort(Comparator<? super BabyInfo> comparator) {
- Collections.sort(mBabyInfoList, comparator);
- if (mNotifyOnChange)
- notifyDataSetChanged();
- }
- /**
- * 更改数据
- *
- * @param babyInfoList
- */
- public void updateDataSource(List<BabyInfo> babyInfoList) {
- clear();
- mBabyInfoList = babyInfoList;
- }
使用方法:
- public class BabyInfo {
- /**
- * 构造函数
- *
- * @param babyId
- * @param babyName
- * @param birthDate
- * @param gender
- * @param createTime
- * @param updateTime
- * @param imgUri
- * @param weight
- * @param height
- * @param note
- */
- public BabyInfo(int babyId, String babyName, long birthDate,
- long createTime, long updateTime, int imgResource, float
- weight, float height, String note) {
- super();
- this.babyId = babyId;
- this.babyName = babyName;
- this.birthDate = birthDate;
- this.createTime = createTime;
- this.updateTime = updateTime;
- this.imgResource = imgResource;
- this.weight = weight;
- this.height = height;
- this.note = note;
- }
- public int getBabyID() {
- return babyId;
- }
- public void setBabyID(int babyID) {
- this.babyId = babyID;
- }
- public String getBabyName() {
- return babyName;
- }
- public void setBabyName(String babyName) {
- this.babyName = babyName;
- }
- public long getBirthDate() {
- return birthDate;
- }
- public void setBirthDate(long birthDate) {
- this.birthDate = birthDate;
- }
- public long getCreateTime() {
- return createTime;
- }
- public void setCreateTime(long createTime) {
- this.createTime = createTime;
- }
- public long getUpdateTime() {
- return updateTime;
- }
- public void setUpdateTime(long updateTime) {
- this.updateTime = updateTime;
- }
- public int getImgResource() {
- return imgResource;
- }
- public void setImgResource(int imgResource) {
- this.imgResource = imgResource;
- }
- public float getWeight() {
- return weight;
- }
- public void setWeight(float weight) {
- this.weight = weight;
- }
- public float getHeight() {
- return height;
- }
- public void setHeight(float height) {
- this.height = height;
- }
- public void setNote(String note) {
- this.note = note;
- }
- public String getNote() {
- return note;
- }
- private int babyId;
- private String babyName;
- private long birthDate;
- private long createTime;
- private long updateTime;
- private int imgResource;
- private float weight;
- private float height;
- private String note;
- }
方法都很简单,关键是要理解必须重载那几个方法的作用,系统什么时候调用,比如getCount()会在绑定的时 候,首先要确定有多少项,getItemId返回相应行绑定的id,当Item的Click或者长按事件中的参数long itemId,就会调用这个方法返回相应id,绑定的时候,就会循环调用public View getView(int position, View convertView, ViewGroup parent)方法等。
- spinner = (Spinner) findViewById(R.id.Spinner);
- iconSpinnerAdapter = new IconSpinnerAdapter(this,
- R.layout.icon_spinner_dropdown_item, R.id.imageView, R.id.textView);
- List<BabyInfo> babyInfoList = new ArrayList<BabyInfo>(5);
- babyInfoList.add(new BabyInfo(1, "Lucky", 0, 0, 0,
- android.R.drawable.ic_menu_myplaces, 0, 0, null));
- babyInfoList.add(new BabyInfo(2, "Breezy", 0, 0, 0,
- android.R.drawable.ic_menu_my_calendar, 0, 0, null));
- babyInfoList.add(new BabyInfo(3, "Lucy", 0, 0, 0,
- android.R.drawable.ic_menu_today, 0, 0, null));
- babyInfoList.add(new BabyInfo(4, "Emma", 0, 0, 0,
- android.R.drawable.ic_menu_month, 0, 0, null));
- babyInfoList.add(new BabyInfo(5, "Frances ", 0, 0, 0,
- android.R.drawable.ic_menu_compass, 0, 0, null));
- iconSpinnerAdapter.updateDataSource(babyInfoList);
- spinner.setAdapter(iconSpinnerAdapter);
最后看一下效果,Spinner没有控制样式,那个向下箭头的Image已经变形了:
本文转自 breezy_yuan 51CTO博客,原文链接:http://blog.51cto.com/lbrant/486406,如需转载请自行联系原作者