一、自定义adapter
1.创建实体类
public class SpinnerName {
private String name;
public SpinnerName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "SpinnerName{" +
"name='" + name + '\'' +
'}';
}
}
2.定义每个item的布局文件
<?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:background="@drawable/inputbox"
android:orientation="vertical">
<TextView
android:id="@+id/style_spinner_txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:background="@drawable/inputbox" />
</LinearLayout>
3.自定义adapter继承BaseAdapter
public class SpinnerAdapter extends BaseAdapter {
private Context context;
private List<SpinnerName> data;
public SpinnerAdapter(Context context, List<SpinnerName> data) {
this.context = context;
this.data = data;
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return data.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Holder holder;
if (convertView == null) {
holder = new Holder();
convertView = LayoutInflater.from(context).inflate(R.layout.style_spinner, parent, false);
holder.txt = convertView.findViewById(R.id.style_spinner_txt);
convertView.setTag(holder);
} else {
holder = (Holder) convertView.getTag();
}
holder.txt.setText(data.get(position).getName());
return convertView;
}
class Holder {
TextView txt;
}
}
4.在activity中加载adapter
List<SpinnerName> list = new ArrayList<SpinnerName>();
list.add(new SpinnerName("aaa"));
list.add(new SpinnerName("bbb"));
list.add(new SpinnerName("ccc"));
list.add(new SpinnerName("ddd"));
SpinnerAdapter adapter = new SpinnerAdapter(ConfigNetActivity.this, list);
spin.setAdapter(adapter);
二、重写ArrayAdapter的getDropDownView()方法来实现
1.activity中代码
final List<String> list = new ArrayList<>();
list.add("aaa");
list.add("bbb");
list.add("ccc");
list.add("ddd");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(ConfigNetActivity.this,
R.layout.spinner_checked_text, list) {
@Override
public View getDropDownView(int position, View convertView,
ViewGroup parent) {
View view = inflate(getContext(), R.layout.spinner_item_layout,
null);
TextView label = (TextView) view
.findViewById(R.id.spinner_item_label);
label.setText(list.get(position));
if (spin.getSelectedItemPosition() == position) {
view.setBackgroundColor(getResources().getColor(
R.color.spinner_green));
} else {
view.setBackgroundColor(getResources().getColor(
R.color.spinner_light_green));
}
return view;
}
};
adapter.setDropDownViewResource(R.layout.spinner_item_layout);
spin.setAdapter(adapter);
2.spinner_checked_text.xml的代码(注意:必须是CheckedTextView,否则会出错。)
<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:gravity="center_vertical"
android:singleLine="true"
android:textColor="#666666"
android:textSize="18sp">
</CheckedTextView>
3.第二部分就是那个弹出来的下拉窗口,这个是重写ArrayAdapter的getDropDownView()方法来实现的:
final List<String> list = new ArrayList<>();
list.add("aaa");
list.add("bbb");
list.add("ccc");
list.add("ddd");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(ConfigNetActivity.this,
R.layout.spinner_checked_text, list) {
@Override
public View getDropDownView(int position, View convertView,
ViewGroup parent) {
View view = inflate(getContext(), R.layout.spinner_item_layout,
null);
TextView label = (TextView) view
.findViewById(R.id.spinner_item_label);
label.setText(list.get(position));
if (spin.getSelectedItemPosition() == position) {
view.setBackgroundColor(getResources().getColor(
R.color.spinner_green));
} else {
view.setBackgroundColor(getResources().getColor(
R.color.spinner_light_green));
}
return view;
}
};
adapter.setDropDownViewResource(R.layout.spinner_item_layout);
spin.setAdapter(adapter);
其中spinner_item_layout.xml的代码如下:
<?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="match_parent"
android:background="@color/spinner_light_green"
android:padding="5dp">
<TextView
android:id="@+id/spinner_item_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:textColor="@color/text_green"
android:textSize="17sp" />
</RelativeLayout>
颜色示例:
<color name="text_green">#48713a</color>
<color name="spinner_light_green">#e4ead3</color>
<color name="spinner_green">#90ac58</color>