我遇到过同样的问题。对于以后会遇到此问题的人,我找到了解决方案之一。
public class MyActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner spinner = (Spinner)findViewById(R.id.spinner);
ArrayList<String> items = new ArrayList<String>();
for (int i=1; i<6; i++) items.add("Spinner item "+i);
spinner.setAdapter(new SpinnerAdapter(this,R.layout.spinner_item_list,items));
}
public class SpinnerAdapter extends ArrayAdapter<String> {
private ArrayList<Boolean> mChecked;
private ArrayList<String> mValues;
private Context mContext;
public SpinnerAdapter(Context context, int resourceId, ArrayList<String> values) {
super(context, resourceId, values);
mValues = values;
mContext = context;
mChecked = new ArrayList<Boolean>();
for (int i=0; i<mValues.size(); i++){
mChecked.add(false);
}
}
@Override
public View getDropDownView(int position, View convertView,ViewGroup parent) {
View row= View.inflate(mContext,R.layout.spinner_item_list, null);
TextView label=(TextView)row.findViewById(R.id.textView);
label.setText(mValues.get(position));
RadioButton rb = (RadioButton)row.findViewById(R.id.radioButton);
rb.setFocusable(false);
rb.setClickable(false);
rb.setChecked(mChecked.get(position));
return row;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = View.inflate(mContext,R.layout.spinner_item_top, null);
TextView label=(TextView)row.findViewById(R.id.textView);
label.setText(mValues.get(position));
for(int i=0; i<mChecked.size(); i++){
mChecked.set(i,(i==position));
}
return row;
}
}
}
spinner_item_list.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/textView" android:layout_centerVertical="true"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/radioButton" android:layout_alignParentRight="true" android:checked="false"/>
</RelativeLayout>
spinner_item_top.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/textView"></TextView>