Android:在 listView 单击上禁用突出显示
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/2907335/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android: disabling highlight on listView click
提问by John Moffitt
I want to disable the orange highlight that occurs when touching a listView row. So far in my xml I have tried the following:
我想禁用触摸 listView 行时出现的橙色突出显示。到目前为止,在我的 xml 中,我尝试了以下操作:
android:focusable="false"
android:focusableInTouchMode="false"
android:clickable="false"
More information: I want there to be zero difference when a user touches the screen on this listView object.
更多信息:当用户触摸此 listView 对象上的屏幕时,我希望有零差异。
回答by RoflcoptrException
Add this to your xml:
将此添加到您的 xml:
android:listSelector="@android:color/transparent"
And for the problem this may work (I'm not sure and I don't know if there are better solutions):
对于这个问题,这可能有效(我不确定,也不知道是否有更好的解决方案):
You could apply a ColorStateListto your TextView.
您可以将ColorStateList应用于您的 TextView。
回答by Mushtaq Jameel
RoflcoptrException's answer should do the trick,but for some reason it did not work for me, So I am posting the solution which worked for me, hope it helps someone
RoflcoptrException 的答案应该可以解决问题,但由于某种原因它对我不起作用,所以我发布了对我有用的解决方案,希望它可以帮助某人
<ListView 
android:listSelector="@android:color/transparent" 
android:cacheColorHint="@android:color/transparent"
/>
回答by Cheryl Simon
The orange highlight effect is a styleon the ListView. This articlegives a good overview of how to override the listView style.
橙色高亮效果是ListView上的一种样式。 本文很好地概述了如何覆盖 listView 样式。
Essentially, you have a selector that specifies different style elements based on the current state.
本质上,您有一个选择器,可以根据当前状态指定不同的样式元素。
see this for short and quick solution https://stackoverflow.com/a/12242564/185022
请参阅此以获取简短快速的解决方案https://stackoverflow.com/a/12242564/185022
回答by emdog4
From ListView: Disable Focus Highlight,
when you set your ListAdapteruse the following code
当您设置ListAdapter使用以下代码时
ListAdapter adapter = new SimpleCursorAdapter(MyList, Layout, c, 
                new String[] { "Name", "Score" }, to) 
{ 
    public boolean areAllItemsEnabled() 
    { 
        return false; 
    } 
    public boolean isEnabled(int position) 
    { 
        return false; 
    } 
}; 
This will override the BaseAdapterclass.  It also cancels the white border between cells.
这将覆盖BaseAdapter该类。它还取消了单元格之间的白色边框。
回答by sheetal
add this also to ur XMl along with listselector..hope it will work
将此也添加到您的 XMl 和 listselector ..希望它会起作用
<ListView
android:cacheColorHint="@android:color/transparent"
android:listSelector="@android:color/transparent"/> 
回答by Libin
If you are using ArrayAdapteror BaseAdapterto populate the list items. Overridethe isEnabledmethod and return false.
如果您正在使用ArrayAdapter或BaseAdapter填充列表项。Override该isEnabled方法和回报false。
 @Override
  public boolean isEnabled (int position) {
    return false;
  }
回答by aerobrain
After a few 'google'ing and testing on virtual and real devices, I notice my below code works:
在虚拟和真实设备上进行了几次“谷歌”搜索和测试后,我注意到我的以下代码有效:
ArrayAdapter<String> myList = new ArrayAdapter<String>(this, R.layout.list_item, strText) {
    public boolean isEnabled(int position) 
    { 
            return false; 
    } 
};
notice that I've omitted the areAllItemsEnabled()portion.
请注意,我省略了该areAllItemsEnabled()部分。
回答by Dmitry Chistyakov
Nothing helps me but this:
没有什么能帮助我,但这个:
transparent_drawable.xml:
transparent_drawable.xml:
<?xml version="1.0" encoding="utf-8"?>    
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#00000000"/>
</shape>
layout.xml:
layout.xml:
android:listSelector="@drawable/transparent_drawable"
回答by Robert Bae Dong Hwan
in code
在代码中
listView.setSelector(getResources().getDrawable(R.drawable.transparent));
and add small transparent image to drawable folder.
并将小的透明图像添加到可绘制文件夹。
Like: transparent.xml
喜欢:transparent.xml
<?xml version="1.0" encoding="utf-8"?>    
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#00000000"/>
</shape>
回答by Natali
As an alternative:
作为备选:
listView.setSelector(android.R.color.transparent);
or
或者
listView.setSelector(new StateListDrawable());

