Spinner
と呼ばれるカスタムアダプタクラスを使用していAlgorithmAdapter
ますが、原則としてすべてが正常に機能しています。つまり、スピナーポップアップが表示され、含まれているすべてのビューが適切に拡張されます。しかし、私が知ることができないのは、選択が行われたときにスピナーにどのように「伝える」かということです。もちろん、私は知ってsetSelection()
いますが、それはポップアップを破棄してアクティビティにフォーカスを戻すことはありません。
したがって、関連するコードのいくつかを次に示します。
私のアクティビティでは、次のようにアダプタを作成します。
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) {
のgetDropDownView()
方法AlgorithmAdapter
:
@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
は、現在チェックされているものと、チェックを外す必要があるものを管理するための、すべてのドロップダウンビューからのすべてのラジオボタンのリストです。
ポップアップが閉じないという事実を除いて、それはすべて非常にうまく機能しますsetSelection()
(それ自体は機能しています)。
また、スピナーにをアタッチしOnItemClickListener
てそこにある選択項目を管理しようとしましたが、それは例外をスローしますsetOnItemClickListener cannot be used with a spinner
。これは、フレームワークがこの時点で私を助けるのではなく、私の邪魔をしているように感じるので、本当に迷惑です。しかし、とにかく、それが私がスピナーをアダプターに通さなければならない理由です。それは私が別の解決策を持っていて良かったと思います…</ p>
したがって、私が見逃しているのは、スピナードロップダウンを「閉じる」または「破棄する」方法だけです。誰かが私を助けてくれますか?これは道の近くでさえそれをするのですか?スピナー用のカスタムアダプターを作成することは非常に基本的な作業であるため、それほど難しいことではありません。
編集:これが役立つかどうかはわかりませんが、誰かが私のドロップダウンビューアイテムのレイアウトファイルにエラーを見つけた可能性があります。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スピナードロップダウンをどのように閉じ/破棄しますか?