【问题标题】:Spinner with custom adapter doesn't disappear on selection带有自定义适配器的微调器在选择时不会消失
【发布时间】:2012-10-16 15:17:09
【问题描述】:

我正在使用Spinner 和一个名为AlgorithmAdapter 的自定义适配器类,原则上一切正常,这意味着微调器弹出窗口出现并且所有包含的视图都正确膨胀。但是,我无法找到的是在做出选择时我如何“告诉”微调器。我当然知道setSelection(),但这不会释放弹出窗口并将焦点返回给Activity。

所以这里有一些相关的代码:

在我的活动中,我像这样创建我的适配器:

SpinnerAdapter spinnerAdapter = new AlgorithmAdapter(
    this,
    algorithmSpinner,
    R.layout.algo_spinner_item_detail,
    res.getStringArray(R.array.anaglyph_algorithm_titles),
    res.getStringArray(R.array.anaglyph_algorithm_descriptions),
    res.obtainTypedArray(R.array.anaglyph_algorithm_icons),
    algorithmSpinner.getSelectedItemPosition()
);

algorithmSpinner.setAdapter(spinnerAdapter);

AgorithmAdapter 的构造函数具有以下签名:

public AlgorithmAdapter(Context context, Spinner spinner, int viewResourceId, String[] titles,
        String[] descriptions, TypedArray icons, int selectedPosition) {

AlgorithmAdapter的getDropDownView()方法:

@Override
public View getDropDownView(final int position, View convertView, ViewGroup parent) {
    if(convertView == null) {
        LayoutInflater inflater = LayoutInflater.from(context);
        convertView = inflater.inflate(viewResourceId, parent, false);
    }

    final ImageView icon = (ImageView) convertView.findViewById(R.id.algoItemIcon);
    final TextView title = (TextView) convertView.findViewById(R.id.algoItemTitle);
    final TextView description = (TextView) convertView.findViewById(R.id.algoItemDescription);
    final RadioButton radio = (RadioButton) convertView.findViewById(R.id.algoItemRadio);

    icon.setImageDrawable(icons.getDrawable(position));
    title.setText(titles[position]);
    description.setText(descriptions[position]);
    radioGroup.add(radio);

    /* it's essential to uncheck in case the positions do not match, because
     * the system reuses views that could still have a checked radio button */
    radio.setChecked(position == selectedPosition);

    convertView.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View view) {
            // uncheck every other radio button
            for(RadioButton each : radioGroup)
                each.setChecked(false);

            // inform adapter and user that this button is selected now
            radio.setChecked(true);
            selectedPosition = position;

            spinner.setSelection(position);
        }

    });

    return convertView;
}

其中radioGroup 是所有下拉视图中所有 RadioButtons 的列表,以便管理当前选中哪些单选按钮以及因此必须取消选中哪些单选按钮。

除了弹出窗口没有在setSelection() 上关闭(虽然它本身正在工作)之外,这一切都很好。

我还尝试将 OnItemClickListener 附加到 Spinner 并管理其中的选择内容,但这会引发一个异常 setOnItemClickListener cannot be used with a spinner,这真的很烦人,因为感觉好像框架挡住了我的路,而不是在这一点上帮助我。但无论如何,这就是我必须将 Spinner 传递给适配器的原因,我很高兴我有一个不同的解决方案……

所以我唯一缺少的是我如何“关闭”或“处置” Spinners 下拉菜单。有人可以帮我吗?这甚至是接近它的方式吗?这不会那么困难,因为为 Spinners 编写自定义适配器是一项非常基本的任务。

编辑:不知道这是否有帮助,但也许有人在我的下拉视图项的布局文件中发现错误。 algo_spinner_item_detail.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:padding="10dp"
    android:background="@android:drawable/list_selector_background" >

    <ImageView
        android:id="@+id/algoItemIcon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:contentDescription="@string/algoIconDescription"
        android:layout_marginRight="8dp" />

    <RadioButton
        android:id="@+id/algoItemRadio"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignParentRight="true"
        android:layout_marginLeft="10dp"
        android:layout_centerVertical="false" />

    <TextView
        android:id="@+id/algoItemTitle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/algoItemIcon"
        android:layout_alignTop="@id/algoItemIcon"
        android:textStyle="bold"
        android:layout_marginBottom="1dp" />

    <TextView
        android:id="@+id/algoItemDescription"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/algoItemTitle"
        android:layout_alignLeft="@id/algoItemTitle"
        android:layout_toLeftOf="@id/algoItemRadio" />

</RelativeLayout>

在你选择了一个项目后,你到底是如何关闭/处理 f***ing Spinner 下拉菜单的?

【问题讨论】:

  • 我当然可以这样做,但是 1) 也不会处理弹出窗口和 2) 完全省略 convertView 的 OnClickListener 导致 OnItemSelectedListener 不被调用,因为选择只发生在onClick
  • 以上评论是对已删除评论的回应。它建议使用OnItemSelectedListener

标签: android android-widget android-spinner


【解决方案1】:

这就是我的诀窍(警告:不要向您的孩子展示此代码,您肯定会为此下地狱):

在我的convertView的onClick方法中我放了这行代码:

((View) parent.getParent().getParent().getParent().getParent()).setVisibility(View.GONE);

这不仅摆脱了包含 convertViews 的 LinearLayout,而且还使 com.android.internal.policy.impl.PhoneWindow$DecorView 不可见,在显示对话框/下拉菜单时,它会使 Activity 屏幕变暗。

我仍在寻求更好的解决方案。

【讨论】:

  • 我遇到了同样的问题。但是,如果我使用你的解决方案,我遇到了 NPE,没有那么多“父母”可以上去。当我减少到 2 个 getParent() 调用时,我得到了另一个错误“RootViewImpl”无法转换为 View。你找到更好的解决方案了吗?这个旋转怪人真的很烂。
  • 嘿,我有个主意,就是在选择项目后生成一个“返回”键,因为这就是我手动关闭弹出窗口的方式。现在正在寻找如何做到这一点。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多