我花了几天的时间尝试解决我在Android上使用ListViews时遇到的问题。我想使用ListView实现单选列表框。因此,我想只有一行具有预定义的浅色背景颜色,其余行具有另一个预选颜色。我遇到的问题是,当我单击特定行时,突出显示的是另一行而不是我按下的那一行。我添加了几条消息以记录发生了什么,但所有情况似乎都正常。这是我的代码:
public class TryListViewActivity extends Activity {
protected static final int NO_SELECTED_COLOR = 0xFF191919;
protected static final int SELECTED_COLOR = 0xFF3366CC;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ListView listView = new ListView(this);
ArrayList<String> list = new ArrayList<String>();
list.add("Option 1");
list.add("Option 2");
list.add("Option 3");
list.add("Option 4");
list.add("Option 5");
list.add("Option 6");
list.add("Option 7");
list.add("Option 8");
list.add("Option 9");
list.add("Option 10");
list.add("Option 11");
list.add("Option 12");
list.add("Option 13");
list.add("Option 14");
list.add("Option 15");
ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(
this,
R.layout.list_box_entry,
list
);
listView.setAdapter(listAdapter);
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
// Set the listener
listView.setOnItemClickListener(
new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Log.i(
"Log",
"[SingleSelectionListBox] Item clicked: position="
+ position + ";id=" + id
);
// First, set all rows to be unselected
int counter = parent.getCount();
Log.i(
"Log",
"[SingleSelectionListBox] "
+ counter + " items found inside the parent"
);
int children = parent.getChildCount();
Log.i(
"Log",
"[SingleSelectionListBox] "
+ children + " views found inside the parent"
);
for(int i=0;i<children;i++) {
Log.i(
"Log",
"[SingleSelectionListBox] Child "
+ i + " has message "
+ ((TextView)parent.getChildAt(i)).getText()
);
}
// Too inefficient but for now is OK
for(int i=0;i<children;i++)
parent.getChildAt(i)
.setBackgroundColor(NO_SELECTED_COLOR);
Log.i("Log",
"[SingleSelectionListBox] First visible position: "
+ parent.getFirstVisiblePosition()
);
// Set the background color
TextView textView = (TextView)(parent.getChildAt(
position-parent.getFirstVisiblePosition()));
textView.setBackgroundColor(SELECTED_COLOR);
Log.i(
"Log",
"[SingleSelectionListBox] Text inside the "
+ " View changing the color " + textView.getText()
);
}
}
);
setContentView(listView);
}
}
在资源(res/layout)中,我插入了一个名为list_text_entry.xml的文件,其内容如下:
<TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:gravity="center"
android:textColor="#FFFFFF" android:padding="10dp" android:textSize="16sp">
</TextView>
例如,如果我在listView滚动到我看到的第一行是"Option 4"之后再点击"Option 11"条目,那么只有携带蓝色背景的"Option 7"行会被选中。有人能够向我解释这里发生了什么吗?下面是我从日志中获取的信息。
[SingleSelectionListBox] Item clicked: position=10;id=10
[SingleSelectionListBox] 15 items found inside the parent
[SingleSelectionListBox] 11 views found inside the parent
[SingleSelectionListBox] Child 0 has message Option 4
[SingleSelectionListBox] Child 1 has message Option 5
[SingleSelectionListBox] Child 2 has message Option 6
[SingleSelectionListBox] Child 3 has message Option 7
[SingleSelectionListBox] Child 4 has message Option 8
[SingleSelectionListBox] Child 5 has message Option 9
[SingleSelectionListBox] Child 6 has message Option 10
[SingleSelectionListBox] Child 7 has message Option 11
[SingleSelectionListBox] Child 8 has message Option 12
[SingleSelectionListBox] Child 9 has message Option 13
[SingleSelectionListBox] Child 10 has message Option 14
[SingleSelectionListBox] First visible position: 3
[SingleSelectionListBox] Text inside the View changing the color Option 11
我猜测所有子元素在ViewGroup中都是从上到下排序的,即使我在代码中这样做:
TextView textView = (TextView)(parent.getChildAt(
position-parent.getFirstVisiblePosition()
));
textView.setBackgroundColor(SELECTED_COLOR);
出现消息
选项11
,但实际上选择的是 选项7
。这是Android的一个错误吗?