我正在使用一个 Spinner
和一个名为 AlgorithmAdapter
的自定义适配器类,原则上一切正常,这意味着会出现微调器弹出窗口并且所有包含的 View 都已正确膨胀。但是,我无法找到的是在做出选择时我是如何“告诉”微调器的。我当然知道 setSelection()
,但这不会处理弹出窗口并将焦点返回到 Activity。
下面是一些相关的代码:
在我的 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
是所有下拉 View 中的所有 RadioButton 的列表,以便管理当前选中的和取消选中的。
除了弹出窗口不会在 setSelection()
上关闭(虽然它本身正在工作)之外,一切都很好。
我还尝试将 OnItemClickListener
附加到 Spinner 并管理其中的选择内容,但这会引发异常,提示 setOnItemClickListener cannot be used with a spinner
,这真的很烦人,因为感觉框架似乎挡住了我的路,而不是在这一点上帮助我。但无论如何,这就是我必须将 Spinner 传递给适配器的原因,我很高兴我有一个不同的解决方案......
所以我唯一缺少的是如何“关闭”或“处理”Spinners 下拉菜单。有人可以帮我吗?这甚至接近于解决它的方式吗?这不会那么困难,因为为 Spinners 编写自定义适配器是一项非常基本的任务。
编辑:不知道这是否有帮助,但也许有人在我的下拉 View 项目的布局文件中发现错误。 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>
在你选择一个项目后,你到底是如何关闭/处理该死的 Spinner 下拉菜单的?
最佳答案
这就是我的诀窍(警告:不要向您的 child 展示此代码,您肯定会为此下 hell ):
在我的 convertView
的 onClick
方法中,我放置了这行代码:
((View) parent.getParent().getParent().getParent().getParent()).setVisibility(View.GONE);
这不仅摆脱了包含 convertView
的 LinearLayout,而且还使 com.android.internal.policy.impl.PhoneWindow$DecorView
不可见的,当显示对话框/下拉菜单时,它会使 Activity 屏幕变暗。
我仍在寻找更好的解决方案。
关于android - 带有自定义适配器的微调器不会在选择时消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12917963/