android實踐專案七自定義的Spinner

tangsilian發表於2016-04-20

程式實現的效果
如下
話不多說先上子佈局

<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    android:orientation="horizontal" >  

    <TextView  
        android:id="@+id/textView1"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:drawableLeft="@drawable/ic_launcher"  
        android:paddingRight="8dip"  
        android:paddingTop="8dip"  
        android:text="TextView"  
        android:textSize="25sp" />  

    <TextView  
        android:id="@+id/textView2"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:paddingLeft="8dip"  
        android:paddingTop="8dip"  
        android:text="TextView"  
        android:textSize="25sp" />  

</LinearLayout>  

構造一個peeson類來封裝資料

package com.example.demotest;

public class Person {  
    private String personName;  
    private String personAddress;  
    public Person(String personName, String personAddress) {  
        super();  
        this.personName = personName;  
        this.personAddress = personAddress;  
    }  
    public String getPersonName() {  
        return personName;  
    }  
    public void setPersonName(String personName) {  
        this.personName = personName;  
    }  
    public String getPersonAddress() {  
        return personAddress;  
    }  
    public void setPersonAddress(String personAddress) {  
        this.personAddress = personAddress;  
    }  

}  

再來寫最重要的資料介面卡(自定義元件的核心)

package com.example.demotest;

 import java.util.List;  
import android.content.Context;  
import android.view.LayoutInflater;  
import android.view.View;  
import android.view.ViewGroup;  
import android.widget.BaseAdapter;  
import android.widget.TextView;  

public class MySpinnerAdapter extends BaseAdapter {  
    private List<Person> mList;  
    private Context mContext;  

    public MySpinnerAdapter(Context pContext, List<Person> pList) {  
        this.mContext = pContext;  
        this.mList = pList;  
    }  

    @Override  
    public int getCount() {  
        return mList.size();  
    }  

    @Override  
    public Object getItem(int position) {  
        return mList.get(position);  
    }  

    @Override  
    public long getItemId(int position) {  
        return position;  
    }  
    /** 
     * 下面是重要程式碼 
     */  
    @Override  
    public View getView(int position, View convertView, ViewGroup parent) {  
        LayoutInflater _LayoutInflater=LayoutInflater.from(mContext);  
        convertView=_LayoutInflater.inflate(R.layout.spinner3, null);  
        if(convertView!=null)  
        {  
            TextView _TextView1=(TextView)convertView.findViewById(R.id.textView1);  
            TextView _TextView2=(TextView)convertView.findViewById(R.id.textView2);  
            _TextView1.setText(mList.get(position).getPersonName());  
            _TextView2.setText(mList.get(position).getPersonAddress());  
        }  
        return convertView;  
    }  
}  

看重寫的getview方法用佈局構建器載入子佈局在例項化自佈局裡面的Textview 表示還可以用viewholder優化

最後再看主要的活動

package com.example.demotest;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Spinner;

public class SpinnerTest extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.spinner4);
        Spinner mSpinner = (Spinner) findViewById(R.id.spinnertest);
        // 建立資料來源
        List<Person> persons = new ArrayList<Person>();
        persons.add(new Person("張三", "上海 "));
        persons.add(new Person("李四", "上海 "));
        persons.add(new Person("王五", "北京"));
        persons.add(new Person("趙六", "廣州 "));
        // 建立Adapter繫結資料來源
        MySpinnerAdapter _MyAdapter = new MySpinnerAdapter(this, persons);
        // 繫結Adapter
        mSpinner.setAdapter(_MyAdapter);
    }
}

程式碼很簡單 初始化控制元件 和資料 再給Spinner載入介面卡就好了

主佈局檔案就只是一個Spinner

<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    android:orientation="horizontal" >  

 <Spinner 
     android:id="@+id/spinnertest"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:spinnerMode="dialog"
     />

</LinearLayout>  

Spinner的彈出屬性在android:spinnerMode中設定
很簡單的案例 看完跑一遍吧~

相關文章