是的,从iOS开发人员的角度来看,我发现应用诸如“启动时设置默认选择”和“记住用户点击行后的选择状态”等功能到ListView非常困难.
所以让我们先从“记住选择”开始.问题是,即使你知道这一点
您可以使用选择器xml来定义高亮/按下/焦点样式.但该样式不会
用户点击该行后保留.例如,我有一个突出显示选择器xml(res / drawable文件夹下的list_selector.xml)(但是你可能有其他字段需要突出显示文本视图的文本颜色):
和list_selector_pressed.xml定义了突出显示样式 – 设置背景颜色
为灰色:
所以@David Hedlund建议:
Rather, assign an OnItemClickListener, and have it store away the id of the selected item into some variable.
您需要在类的顶部创建一个实例变量:
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是否为null或当前单击的视图.我们首先通过调用方法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()中.
但是如果你运行它,你会得到NullPointer异常,为什么?
因为此时,列表视图尚未呈现,Android不喜欢具有viewWillAppear的iOS.所以你必须创建一个即时变量来记住它是第一次呈现listview单元格,而在onListItemClick中是为了取消设置该变量:
所以在currentSelectedView声明下:
private Boolean firstTimeStartup = true;
然后添加方法:假设我们要突出显示列表视图中的第一行:
public class HomeAdapter extends ArrayAdapter {
int layoutResourceId;
public HomeAdapter(Context context, int textViewResourceId,
ArrayList 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 ?