public class SpinnerAdapter extends ArrayAdapter {
private Context mContext;
private ArrayList listState;
public SpinnerAdapter(Context context, int resource,
ArrayList objects) {
super(context, resource, objects);
this.mContext = context;
this.listState = objects;
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
return getCustomView(position, convertView, parent, true);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return getCustomView(position, convertView, parent, false);
}
@SuppressLint("InflateParams")
public View getCustomView(int position, View convertView, ViewGroup parent, boolean isDropDown) {
final ViewHolder holder;
if (convertView == null) {
LayoutInflater layoutInflator = LayoutInflater.from(mContext);
convertView = layoutInflator.inflate(R.layout.spinner_item_layout,
null);
holder = new ViewHolder();
holder.mTextView = (TextView) convertView.findViewById(R.id.text);
holder.mCheckedImage = (ImageView) convertView
.findViewById(R.id.checkbox);
holder.mBgLayout= (RelativeLayout) convertView
.findViewById(R.id.bg);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.mTextView.setText(listState.get(position).getTitle());
if (isDropDown) {
if (listState.get(position).isSelected()) {
holder.mCheckedImage.setVisibility(View.VISIBLE);
holder.mBgLayout.setBackgroundColor(android.Color.BLUE); // not sure if the syntax is correct. you need to check this line
} else {
holder.mCheckedImage.setVisibility(View.INVISIBLE);
holder.mBgLayout.setBackgroundColor(android.Color.YELLOW); // not sure if the syntax is correct. you need to check this line
}
}
return convertView;
}
private class ViewHolder {
private TextView mTextView;
private ImageView mCheckedImage;
private RelativeLayout mBgLayout;
}
public void setSpinnerAdapter(ArrayList spinnerItems) {
this.listState = spinnerItems;
notifyDataSetChanged();
}
}
private void setBottelCountData() {
final String[] select_qualification = { "", "1", "2",
"3" };
bottelCountList = new ArrayList<>();
for (int i = 0; i < select_qualification.length; i++) {
SpinnerItem spinnerItem = new SpinnerItem();
spinnerItem.setTitle(select_qualification[i]);
if (i == 2) {
spinnerItem.setSelected(false);
} else {
spinnerItem.setSelected(false);
}
bottelCountList.add(spinnerItem);
}
bottelCountAdapter = new SpinnerAdapter(getActivity(), 0,
bottelCountList);
bottelCountAdapter.setDropDownViewResource(R.layout.spinner_item_layout);
bottel_Count_Spinner.setAdapter(bottelCountAdapter);
bottel_Count_Spinner.setSelection(3);
}
bottel_Count_Spinner
.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView> parent,
View view, int position, long id) {
if (position == 0) {
for (int count = 0; count < bottelCountList.size(); count++) {
bottelCountList.get(count).setSelected(false);
}
noOfBottel = 0;
return;
}
bottelCountList.get(position).setSelected(true);
for (int count = 0; count < bottelCountList.size(); count++) {
if (position != count) {
bottelCountList.get(count).setSelected(false);
}
}
bottel_Count_Spinner.setPrompt("Hello");
bottelCountAdapter.setSpinnerAdapter(bottelCountList);
noOfBottel = Integer.parseInt(bottelCountList.get(
position).getTitle());
}
public void onNothingSelected(AdapterView> parent) {
}
});
使用这个xml
android:layout_width="wrap_content"
android:id="@+id/bg"
android:layout_height="wrap_content" >
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/checkbox"
android:layout_centerVertical="true"
android:padding="10dp"/>
android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:padding="10dp"
android:src="@android:drawable/checkbox_on_background"
android:contentDescription="@string/app_name"
android:layout_marginLeft="10dp"
android:layout_alignParentStart="true" />
我希望这会对你有所帮助.如果您发现任何错误,请告知