1

ArrayAdapterSpinner. 不同之处在于,它显示来自ArrayList复杂类的图像而不是纯文本。到目前为止它有效。图像和单选按钮将根据需要显示。问题是,下拉视图的行为不正确:它不会在单击时关闭,并且只有单选按钮是可单击的,而不是整个视图。

有人知道出了什么问题吗?我是否必须listeneradapter?

这是该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);
    }
4

3 回答 3

7

在单选按钮的布局中设置android:focusable="false"

于 2013-02-21T10:42:14.097 回答
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>
于 2013-10-12T14:00:10.007 回答
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;
 }
于 2013-02-21T10:54:38.373 回答