我为ArrayAdapter
创建了自定义Spinner
。不同的是,它显示来自复杂类ArrayList
的图像而不是纯文本。它到目前为止工作。图像和单选按钮根据需要显示。问题是,下拉视图的行为不正确:它不会在单击时关闭,只有单选按钮可以单击而不是整个视图。
有人知道出了什么问题吗?我是否必须在listener
??
adapter
以下是getDropDownView
方法的代码:
@Override
public View getDropDownView(int position, View convertView,
ViewGroup parent) {
LayoutInflater inflater=(LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout view=(LinearLayout)inflater.inflate(R.layout.spinnerimageitem, null);
ImageView iv=(ImageView)view.getChildAt(0);
RadioButton rb=(RadioButton)view.getChildAt(1);
int iImageID=ctx.getResources().getIdentifier(
"f_"+funcs.get(position).getBitmapSetup(),
"drawable", ctx.getPackageName());
if(loco.getFunction(iIndex).equals(funcs.get(position)))
rb.setChecked(true);
iv.setImageResource(iImageID);
return(view);
}
答案 0 :(得分:6)
在单选按钮的布局中设置android:focusable="false"
。
答案 1 :(得分:1)
我有同样的问题。对于那些稍后会遇到这个问题的人,我找到了一个解决方案。
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>
答案 2 :(得分:0)
@Override
public View getDropDownView(int position, View convertView,
ViewGroup parent) {
View view = convertView;
if(view == null){
LayoutInflater inflater=(LayoutInflater)
tx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view=(LinearLayout)inflater.inflate(R.layout.spinnerimageitem, null);
}
/// your code ....
return view;
}