已被点击后,如何禁用ListView项? [英] How to disable ListView item after it has been clicked?
问题描述
我字符串
的简单数组,我是在一个水平的ListView
与<$ C $显示C> ArrayAdapter 。 什么我希望做的是:当用户选择从项目的的ListView
,使该项目不可点击,改变背景颜色该项目。也许像灰显,看它。我一直在寻找为创建自定义的适配器
并重写 isEnabled(INT位置)
方法,但我不知道如何我会去这一点。任何意见,建议或帮助将大大AP preciated感谢!
我一直在寻找为创建一个自定义适配器并重写
isEnabled(INT位置)
方法,但我不知道我怎么会去了解这一点。
块引用>这是很容易做到。我推荐一个SparseBooleanArray跟踪启用的项目效率:
公共类MyAdapter扩展ArrayAdapter&LT;串GT; {
私人SparseBooleanArray enabledItems =新SparseBooleanArray(); 公共MyAdapter(上下文的背景下,INT textViewResourceId,列表与LT;弦乐&GT;对象){
超(背景下,textViewResourceId,对象);
} @覆盖
公共布尔areAllItemsEnabled(){
返回false;
} @覆盖
公共布尔isEnabled(INT位置){
返回enabledItems.get(位置,真正的);
} 公共无效toggleItem(INT位置){
布尔州= enabledItems.get(位置,真正的);
enabledItems.put(位置,状态!);
}
}Eclipse中的自动完成功能所做的工作中必须的,但这里有一些快速的注意事项:
- 您必须重写
areAllItemsEnabled()
与沿isEnabled()
- 我设计
切换()
来通过使用onItemClickListener()
你只需要调用adapter.toggle(位置)
如果你想改变行的外观(不是系统默认的是什么允许和禁止做更多)简单地覆盖
getView()
。不要忘了包括两种情况:公共查看getView(INT位置,查看convertView,父母的ViewGroup){
convertView = super.getView(位置,convertView,父母); 如果(!isEnabled(位置)){
/ *更改为禁用外观* /
}
其他{
/ *恢复默认外观* /
}
返回convertView;
}希望帮助!
I have a simple array of
Strings
that I was displaying in a horizontalListView
with anArrayAdapter
. What I'm looking to do is: when the user selects an item from theListView
, make that item not clickable and change the background color of that item. Perhaps like a "grayed-out" look to it. I was looking into creating a customAdapter
and overriding theisEnabled(int position)
method but I don't know how I would go about this. Any advice, suggestions, or help will be greatly appreciated thanks!解决方案I was looking into creating a custom Adapter and overriding the
isEnabled(int position)
method but I don't know how I would go about this.This is quite easy to do. I recommend a SparseBooleanArray to track the enabled items for efficiency:
public class MyAdapter extends ArrayAdapter<String> { private SparseBooleanArray enabledItems = new SparseBooleanArray(); public MyAdapter(Context context, int textViewResourceId, List<String> objects) { super(context, textViewResourceId, objects); } @Override public boolean areAllItemsEnabled() { return false; } @Override public boolean isEnabled(int position) { return enabledItems.get(position, true); } public void toggleItem(int position) { boolean state = enabledItems.get(position, true); enabledItems.put(position, !state); } }
The AutoComplete feature of Eclipse did must of the work, but here are some quick notes:
- You must override
areAllItemsEnabled()
along withisEnabled()
- I designed
toggle()
to be used by anonItemClickListener()
you only need to calladapter.toggle(position)
If you want to change the row's appearance (more than what enabling and disabling does by default) simply override
getView()
. Don't forget to cover both cases:public View getView(int position, View convertView, ViewGroup parent) { convertView = super.getView(position, convertView, parent); if(!isEnabled(position)) { /* change to disabled appearance */ } else { /* restore default appearance */ } return convertView; }
Hope that helps!
这篇关于已被点击后,如何禁用ListView项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!