呀,从iOS开发者的角度来看,我觉得这是非常难以应用像功能“组默认选择启动时”和“记得选择状态用户点击一行后”到ListView控件。
因此,让我们开始“记住选择”第一。问题是,即使你知道 你可以选择使用XML来定义亮点/压/聚焦style.But这种风格不会 保持用户后点击该行。举例来说,我有(下RES /绘制的文件夹list_selector.xml)一个高亮选择这样的XML(但你可能有其他领域需要突出像行的TextView的文本颜色):
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/list_selector_pressed" android:state_pressed="true" />
<item android:drawable="@drawable/list_selector_pressed" android:state_selected="true" />
</selector>
和list_selector_pressed。其中限定的突出显示风格的xml - 设置背景颜色 到灰色:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/dark_gray" />
</shape>
</item>
</layer-list>
因此,作为@大卫Hedlund的建议:
而是,分配一个OnItem ClickListener,并让它将选定项目的ID存储到某个变量中。
您需要创建类的顶部的实例变量:
private View currentSelectedView;
然后去
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
if (currentSelectedView != null && currentSelectedView != v) {
unhighlightCurrentRow(currentSelectedView);
}
currentSelectedView = v;
highlightCurrentRow(currentSelectedView);
//other codes
}
很简单:我们检查,如果currentSelectedView为空或电流点击视图或不。我们首先通过调用方法unhighlightCurrentRow(currentSelectedView)来忽略任何样式---您可能想知道为什么我们将即时变量currentSelectedView作为参数传递,稍后我会解释它。然后我们将视图分配给currentSelectedView并突出显示当前行;以便在用户点击完成后样式将保持不变。
private void unhighlightCurrentRow(View rowView) {
rowView.setBackgroundColor(Color.TRANSPARENT);
TextView textView = (TextView) rowView.findViewById(R.id.menuTitle);
textView.setTextColor(getResources().getColor(R.color.white));
}
private void highlightCurrentRow(View rowView) {
rowView.setBackgroundColor(getResources().getColor(
R.color.dark_gray));
TextView textView = (TextView) rowView.findViewById(R.id.menuTitle);
textView.setTextColor(getResources().getColor(R.color.yellow));
}
啊哈,就是这样。这就是我们如何实现列表视图的“记忆选择”。正如你看到的, 我们要复制代码都在XML和Java代码样式 - 非常愚蠢:(
下一页关于“设置默认选择”你可能会认为你可以做到这一点
。在片段中
listView.setAdapter(adatper)
listView.setSelection(0);
currentSelectedView = listView.getChildAt(0);
highlightCurrentRow(currentSelectedView);
的onCreate()在活动或onActivityCreated()
。
但是,如果你运行它,你会得到空指针异常,为什么? 因为此时,列表视图尚未呈现和Android不像有viewWillAppear的iOS,所以你必须创建一个即时变量来记住它是否是第一次渲染的ListView细胞和onListItemClick取消设置变量:所以下currentSelectedView声明
:
private Boolean firstTimeStartup = true;
然后添加方法:假设我们想强调在列表视图中的第一行:
public class HomeAdapter extends ArrayAdapter<String> {
int layoutResourceId;
public HomeAdapter(Context context, int textViewResourceId,
ArrayList<String> objects) {
super(context, textViewResourceId, objects);
layoutResourceId = textViewResourceId;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(
layoutResourceId, null);
}
if (firstTimeStartup && postion == 0) {
highlightCurrentRow(convertView);
} else {
unhighlightCurrentRow(convertView);
}
TextView title = (TextView) convertView
.findViewById(R.id.menuTitle);
title.setText(getItem(position));
return convertView;
}
}
很简单。 但是你需要在onListItemClick方法的一些变化:
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
if (firstTimeStartup) {// first time highlight first row
currentSelectedView = l.getChildAt(0);
}
firstTimeStartup = false;
if (currentSelectedView != null && currentSelectedView != v) {
unhighlightCurrentRow(currentSelectedView);
}
currentSelectedView = v;
highlightCurrentRow(currentSelectedView);
//other codes
}
你去那里!享受Android :)
我已经尝试在返回时使用setSelection(),但它不好 - 首先所有行都没有突出显示(没有背景色),第二行位于屏幕的顶部,尽管当我离开时它位于中间某处。 谢谢。 – WhiteTigerK 2010-01-11 14:32:01
另一件事 - 在设备的联系人列表中,当使用轨迹球选择联系人,然后按轨迹球查看其详细信息时,联系人列表的状态将被保存,因此返回联系人列表时,联系人仍处于选中状态在屏幕上的位置是相同的。所以我认为这种行为是可以实现的。 谢谢。 – WhiteTigerK 2010-01-11 14:36:33
是的,选择状态将被保存,但如果在任何时候您触摸屏幕,您将切换*触摸模式*,并且在触摸模式下,如您已经注意到的,没有选择项目的视觉反馈只有当物品被触摸时才提供简短的反馈,当你返回到列表视图时这不会发生)。这是默认行为。不要改变它。 – 2010-01-12 10:21:29