自定義TintSpinner的樣式

首先爲啥要用TintSpinner 而不是Spinner?

參見使用AppCompat_v7 21.0.0d的幾個兼容問題

然後是我們要自定義的效果是



也就是說選中的樣式是白色的文字,下拉的樣式是黑色的文字,這樣一個小小的需要,也很需要技巧。

首先,TintSpinner的adapter需要使用ArrayAdapter,而不能使用BaseAdapter,之前我走的彎路是想用BaseAdapter然後在找

adapter.setDropDownViewResource(R.layout.drop_down_item);

方法,結果顯然易見,BaseAdapter更不就沒有這個方法,爲啥?


因爲BaseAdapter實現的是ListAdapter, SpinnerAdapter接口:

public abstract class BaseAdapter implements ListAdapter, SpinnerAdapter

而ArrayAdapter是BaseAdapter的子類

public class ArrayAdapter<T> extends BaseAdapter implements Filterable

所以看到在找不到setDropDownViewResource方法。


疑惑解除了,下面就看下怎麼實現了這個adapter了:

model爲

public class EndemicArea {

    @JsonField("description")
    private String description;
    @JsonField("name")
    private String name;
    @JsonField("pk")
    private int pk;

    public EndemicArea(String description, String name, int pk) {
        this.description = description;
        this.name = name;
        this.pk = pk;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getPk() {
        return pk;
    }

    public void setPk(int pk) {
        this.pk = pk;
    }

}

Adapter爲:

public class EndemicAreaSpinnerAdapter extends ArrayAdapter<EndemicArea> {


    private LayoutInflater layoutInflater;

    public EndemicAreaSpinnerAdapter(Context context, int resource, List<EndemicArea> endemicAreaList) {
        super(context, resource, endemicAreaList);
        layoutInflater = LayoutInflater.from(context);

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = layoutInflater.inflate(R.layout.choose_bed_item, null);
        TextView areaName = (TextView) view.findViewById(R.id.choose_bed_item_name);
        areaName.setText(getItem(position).getName());
        return view;
    }

    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {
        View view = layoutInflater.inflate(android.R.layout.simple_spinner_dropdown_item, parent, false);
        CheckedTextView areaName = (CheckedTextView) view.findViewById(android.R.id.text1);
        areaName.setText(getItem(position).getName());
        return view;
    }
}

choose_bed_item.xml爲

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/choose_bed_item_name"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="4dp"
    android:gravity="center"
    android:paddingBottom="10dp"
    android:layout_marginRight="8dp"
    android:paddingTop="10dp"
    android:textColor="@color/white"
    android:textSize="20sp">

</TextView>

最後:

 tintSpinner = new TintSpinner(getActivity());
        tintSpinner.setBackgroundResource(R.drawable.abc_spinner_mtrl_am_alpha);
        tintSpinner.setAdapter(spinnerAdapter);

注意:TintSpinner來着import android.support.v7.internal.widget.TintSpinner;

看來Spinner需要ArrayAdapter才能定製下拉的樣式,然後就可以完全自定義了。




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章