3

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スピナードロップダウンをどのように閉じ/破棄しますか?

4

1 に答える 1

0

これが私にとってのトリックでした(警告:このコードを子供に見せないでください、あなたは確かにそれのために地獄に行くでしょう):

私のonClick方法では、convertView次のコード行を記述します。

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

convertViewこれは、 sが含まれているLinearLayoutを取り除くだけでなくcom.android.internal.policy.impl.PhoneWindow$DecorView、ダイアログ/ドロップダウンが表示されているときにアクティビティ画面を暗くするためにそこにあった非表示も作成します。

私はまだより良い解決策を求めています。

于 2012-10-17T13:54:46.953 に答える