天天看點

自定義Spinner之IconSpinner

 關于Spinner的解釋及基本用法,已經可以找到很多例子:

<a href="http://yilee.info/android-spinner.html">http://yilee.info/android-spinner.html</a>

這不是本文的重點,本範例主要通過重寫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,實作上面四個必須覆寫的方法:<b></b>

public class IconSpinnerAdapter extends BaseAdapter { 

    private Context mContext; 

    private int mDropDownResource; 

    private boolean mNotifyOnChange = true; 

    private List&lt;BabyInfo&gt; mBabyInfoList; 

    private LayoutInflater mInflater; 

    private int mResource; 

    private int mTextViewResourceId; 

    private int mImageViewResourceId; 

    private ArrayList&lt;BabyInfo&gt; mOriginalValues; 

    private Object mLock; 

    @Override 

    public int getCount() { 

        return mBabyInfoList.size(); 

    } 

    public Object getItem(int position) { 

        return mBabyInfoList.get(position); 

    public long getItemId(int position) { 

        return mBabyInfoList.get(position).getBabyID(); 

    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&lt;BabyInfo&gt;(); 

        mImageViewResourceId = imageViewResourceId; 

        mTextViewResourceId = textViewResourceId; 

            int imageViewResourceId, int textViewResourceId, 

            List&lt;BabyInfo&gt; babyInfoList) { 

        mBabyInfoList = babyInfoList; 

    /** 

     * 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; 

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

        return createViewFromResource(position, convertView, parent, 

                mDropDownResource); 

當然,為了使功能更加強大,可以重寫其他方法:

/** 

     * 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(); 

            } 

            mBabyInfoList.add(babyInfo); 

            if (mNotifyOnChange) 

                notifyDataSetChanged(); 

     *            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) { 

                mOriginalValues.add(index, babyInfo); 

            mBabyInfoList.add(index, babyInfo); 

     * Removes the specified object from the array. 

     *            The baby info to remove. 

    public void remove(BabyInfo babyInfo) { 

                mOriginalValues.remove(babyInfo); 

            mBabyInfoList.remove(babyInfo); 

        if (mNotifyOnChange) 

            notifyDataSetChanged(); 

     * Remove all elements from the list. 

    public void clear() { 

                mOriginalValues.clear(); 

            mBabyInfoList.clear(); 

     * 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&lt;? super BabyInfo&gt; comparator) { 

        Collections.sort(mBabyInfoList, comparator); 

     * 更改資料 

     * @param babyInfoList 

    public void updateDataSource(List&lt;BabyInfo&gt; babyInfoList) { 

        clear(); 

使用到一個自定義的BabyInfo類,懶得修改,直接拿過來用了,你可以封裝其他對象:

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) { 

    public long getBirthDate() { 

        return birthDate; 

    public void setBirthDate(long birthDate) { 

    public long getCreateTime() { 

        return createTime; 

    public void setCreateTime(long createTime) { 

    public long getUpdateTime() { 

        return updateTime; 

    public void setUpdateTime(long updateTime) { 

    public int getImgResource() { 

        return imgResource; 

    public void setImgResource(int imgResource) { 

    public float getWeight() { 

        return weight; 

    public void setWeight(float weight) { 

    public float getHeight() { 

        return height; 

    public void setHeight(float height) { 

    public void setNote(String 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; 

}

使用方法:

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

iconSpinnerAdapter = new IconSpinnerAdapter(this, 

R.layout.icon_spinner_dropdown_item, R.id.imageView, R.id.textView); 

List&lt;BabyInfo&gt; babyInfoList = new ArrayList&lt;BabyInfo&gt;(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); 

方法都很簡單,關鍵是要了解必須重載那幾個方法的作用,系統什麼時候調用,比如getCount()會在綁定的時

候,首先要确定有多少項,getItemId傳回相應行綁定的id,當Item的Click或者長按事件中的參數long

itemId,就會調用這個方法傳回相應id,綁定的時候,就會循環調用public View getView(int

position, View convertView, ViewGroup parent)方法等。

最後看一下效果,Spinner沒有控制樣式,那個向下箭頭的Image已經變形了:

<a href="http://blog.51cto.com/attachment/201101/102733869.png" target="_blank"></a>

<a href="http://down.51cto.com/data/2357518" target="_blank">附件:http://down.51cto.com/data/2357518</a>

本文轉自 breezy_yuan 51CTO部落格,原文連結:http://blog.51cto.com/lbrant/486406,如需轉載請自行聯系原作者

繼續閱讀