0

Android クイズ プログラムで選択した行を強調表示しようとしています。しかし、私は奇妙な振る舞いをしています。基本的に、onListItemClick に渡された位置はバッキング データ配列内の位置を正しく反映していますが、明らかに子インデックスを使用して検索されたビューは間違っています。子インデックスは、スタックのように位置が逆になっているようです。この仮定を使用して、コードを機能させることができますが、直感に反します。

コードは次のとおりです。

public class QuestionActivity extends ListActivity {

...

        /*
         * Create the adapter
         */
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                R.layout.simple_list_item_single_choice_custom,
                mAppState.mAnswerArray) {
        };

        /* attach the adapter to the ListView */
        setListAdapter(adapter);

        /* set ListView's choice mode to single */
        getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
    }

....

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);


    // always four rows; assume last row is zeroth child. Therefore,
    // if the first row is chosen (0 position) you need to get child 3.
    // 3 - 0 = 3.

    int childCount = l.getChildCount();
    int lastViewPos = childCount - 1;
    View selView = l.getChildAt(lastViewPos - position);
}

...

    <TextView android:id="@+id/display_question"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:textSize="40sp" android:typeface="normal" android:padding="10dp"
        android:layout_gravity="center_vertical|center_horizontal"
        android:textColor="@color/text_color" />
    />

    <ListView android:id="@+id/android:list"
        android:cacheColorHint="#00000000" android:layout_height="wrap_content"
        android:layout_width="fill_parent" android:textColor="@color/text_color"
        android:divider="#00000000" android:dividerHeight="0sp"
        android:focusable="false">
    </ListView>
    <TextView android:id="@+id/display_english"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:paddingLeft="20dp" android:layout_gravity="center_vertical|center_horizontal"
        android:textSize="30sp" android:textColor="@color/text_color" />

    <Button android:id="@+id/next_button1" android:text="@string/next_question"
        android:textSize="15dp" android:layout_gravity="center_vertical|center_horizontal"
        android:textColor="@color/text_color" android:background="@drawable/custom_button"
        android:layout_width="wrap_content" android:layout_height="wrap_content" />
</LinearLayout>

... ここに simple_list_item_single_choice_custom.xml があります

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/text1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textSize="30sp" 
    android:gravity="center_vertical"
    android:checkMark="?android:attr/listChoiceIndicatorSingle"
    android:paddingLeft="20dip" 
    android:textColor="@color/text_color"
    android:background="@drawable/custom_list"
/>

また、onClick イベントでスーパーを呼び出さず、コードからリスト ビューに stackFromBottom を true と false の両方で設定しようとしましたが、どちらも違いはないようでした。

これを行うためのより満足のいく、直感的な方法は何ですか?

4

3 に答える 3

1

男、私はこれを逃したなんて信じられない。実際の質問に適切な注意を払っていませんでした。

クリックされた項目に selView の値を設定しようとしていますね。それが onListItemClicked の「View v」パラメータです。

View selView = v;

あなたが探していることをします。

getChildAt() は、ViewGroup から継承されているため、期待どおりに動作しません。ListView では、ViewGroup に他の要素 (ヘッダーやフッターなど) が存在する可能性があるため、必ずしもリスト項目だけに対応するとは限りません。要するに、あなたが使用している方法で使用することを意図したものではありませんでした. この場合の位置に反比例するのは、おそらく単なる偶然です。

于 2011-05-21T17:33:56.890 に答える
0

android:stackFromBottomが指定されている可能性はありますか? それによって getChildAt の動作が変わるかどうかはわかりません (そして、現時点でテストするものは何もありません) が、それは私の最初の推測です。

于 2011-05-20T23:22:19.757 に答える
0

上から開始するように設定する必要がありますが、1 から開始します (配列インデックスなどとは異なります)。

于 2011-05-20T23:24:24.260 に答える