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 の両方で設定しようとしましたが、どちらも違いはないようでした。
これを行うためのより満足のいく、直感的な方法は何ですか?