Android 将 ListView 中的选择从橙色更改为绿色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2170531/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:48:42  来源:igfitidea点击:

Change Selection in a ListView from Orange to Green

androidandroid-listview

提问by jax

How do I do this per selected list item.

我如何为每个选定的列表项执行此操作。

I tried adding this to android:background

我尝试将其添加到 android:background

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true" 
        android:state_pressed="false"
        android:drawable="@color/android_green" />
    <item android:state_focused="true" 
        android:state_pressed="true"
        android:drawable="@color/black_alpha" />
    <item android:state_focused="false" 
        android:state_pressed="true"
        android:drawable="@color/black_alpha" />
    <item android:drawable="@color/white_alpha" />
</selector> 

but it does not work, it changes the entire ListView.

但它不起作用,它改变了整个 ListView。



Still not working, here is what I have so far

仍然没有工作,这是我目前所拥有的

::listview_background

::listview_background

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true" android:drawable="@color/black_alpha" />
    <item android:drawable="@color/white_alpha" />  
</selector> 

::My view that is using the above

::我的观点是使用上面的

<ListView android:id="@+id/list" android:layout_width="fill_parent"
    android:layout_height="fill_parent" android:layout_below="@+id/round"
    android:listSelector="@drawable/listview_background">
</ListView>

The problem is that the black_alpha will apply the the entire list, not just the selected list item

问题是 black_alpha 将应用整个列表,而不仅仅是选定的列表项

回答by Jay Askren

Here's a good articleon how to use selectors with lists.

这是一篇关于如何在列表中使用选择器的好文章

Instead of setting it to be the android:backgroundof the ListView, I believe you want to set android:listSelectoras shown below:

而不是将其设置android:background为 ListView 的,我相信您希望设置android:listSelector如下所示:

<ListView android:id="@+id/list" 
      android:layout_width="fill_parent"
      android:layout_height="wrap_content" 
      android:layout_gravity="center"
      android:divider="@null" 
      android:dividerHeight="0dip"
      android:listSelector="@drawable/list_selector" />

回答by b-ryce

Jax, you said that:

贾克斯,你说:

The problem is that the black_alpha will apply the the entire list, not just the selected list item

问题是 black_alpha 将应用整个列表,而不仅仅是选定的列表项

I had the same problem. After playing around with it for a while, I was able to get it to stop applying the color to the whole list by using a 3x3 png image of the color I wanted instead of applying a color. So for some weird reason if you use a color you get the odd background behavior, when you use a png, it works fine. :S

我有同样的问题。在玩了一段时间后,我能够通过使用我想要的颜色的 3x3 png 图像而不是应用颜色来让它停止将颜色应用于整个列表。因此,出于某种奇怪的原因,如果您使用一种颜色,您会得到奇怪的背景行为,当您使用 png 时,它可以正常工作。:S

So that is one way to get around it.

所以这是绕过它的一种方法。

-----Another way----------

- - -其它的办法 - - - - -

I found the best results when I set the listView selector to not have any states like this:

当我将 listView 选择器设置为没有任何这样的状态时,我发现了最好的结果:

res/drawable/list_selector.xml

res/drawable/list_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
</selector>

Then I take care of all the states in the background of the list item. NOT in the selector. I found I had more control this way.

然后我会处理列表项背景中的所有状态。不在选择器中。我发现这样我有更多的控制权。

res/color/list_item_bg.xml

res/color/list_item_bg.xml

<?xml version="1.0" encoding="utf-8"?>
   <selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="false"
    android:state_pressed="false" 
    android:drawable="@color/list_item_black_translucent" />
<item android:state_pressed="true" 
    android:drawable="@color/light_green_accent_translucent" />
</selector>

Then here is my list item:

然后这是我的列表项:

res/layout/simple_list_item.xml

res/layout/simple_list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingRight="45dip"
android:paddingBottom="2dip"
android:layout_gravity="center_vertical"
android:background="@color/list_item_bg">
  <ImageView
    android:id="@+id/arrow"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginRight="8dip"
    android:src="@drawable/list_item_arrow" />
 <TextView
android:id="@+id/simple_list_item_label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:textSize="12sp"
android:textColor="@color/list_label_color"/>
</LinearLayout>

回答by Bkteo

I was also facing your problem where the color change applies to the whole list.

我也遇到了您的问题,其中颜色更改适用于整个列表。

But now its working for me now:

但现在它现在对我有用:

My main page containing the listview (noticed I did not apply the listselector here):

我的主页包含列表视图(注意到我没有在这里应用列表选择器):

 <ListView 
    android:id="@+id/controlList" android:layout_width="wrap_content" android:layout_height="wrap_content"/>

Instead applied the listselector to getView, its like tell that particular row (convertView)to use the listselector.xml in drawable foldable when its state changes, e.g. pressed, focused...:

而是将 listselector 应用到 getView,它就像告诉特定行 (convertView)在它的状态发生变化(例如按下、聚焦...)时在可折叠可折叠中使用 listselector.xml:

@Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder;
            if(convertView == null){
                LayoutInflater li = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
                convertView = li.inflate(R.layout.control_list, null);
                viewHolder = new ViewHolder();

                viewHolder.topText = (TextView) convertView.findViewById(R.id.toptext);
                viewHolder.bottomText = (TextView) convertView.findViewById(R.id.bottomtext);
                convertView.setTag(viewHolder);

            }
            else{
                viewHolder = (ViewHolder) convertView.getTag();
            }


                    //each convertView will be assigned the listselector
            convertView.setBackgroundResource(R.drawable.listselector);


            viewHolder.topText.setText(testSchemes[position]);

            //viewHolder.vText.setText(testDetails[position]);
            return convertView;
        }

And finally my listselector.xml:

最后是我的 listselector.xml:

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="false"
        android:state_selected="false"
        android:drawable="@color/lightgreen" />

    <!-- Even though these two point to the same resource, have two states so the drawable will invalidate itself when coming out of pressed state. -->
    <item android:state_pressed="true" 

        android:drawable="@color/darkgreen" />

    <item android:state_selected="true" android:state_pressed="false"
        android:drawable="@color/orange" />



</selector>

This is what the above code does: All entries when first loaded are lightgreen. Scrolling up and down using cursor highlights the selected entry orange. Pressing a row turns it to darkgreen.

这就是上面的代码所做的:第一次加载时所有条目都是浅绿色的。使用光标上下滚动突出显示所选条目橙色。按一行将其变为深绿色。

回答by Ixx

As bigstones mentioned in a comment, a working solution is using a shape instead of a color (I was also getting the whole list highlighted). Here is a complete solution:

正如评论中提到的 bigstones,一个可行的解决方案是使用形状而不是颜色(我还突出显示了整个列表)。这是一个完整的解决方案:

add these 2 attributes to the listview:

将这两个属性添加到列表视图:

android:drawSelectorOnTop="true"
android:listSelector="@drawable/my_list_selector"

add my_list_selector.xml to drawable folder:

将 my_list_selector.xml 添加到 drawable 文件夹:

<?xml version="1.0" encoding="UTF-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> 
    <solid android:color="#77526F35"/>
</shape>

Now the list items should show a semitransparent green overlay on pressed state.

现在列表项应该在按下状态上显示一个半透明的绿色覆盖。

回答by Joan P.S

I faced the same issue and then I just needed a simple line in my item view xml.

我遇到了同样的问题,然后我只需要在我的项目视图 xml 中添加一个简单的行。

    android:background="?android:attr/activatedBackgroundIndicator"

Check this post: This postcould help

检查这篇文章:这篇文章可能会有所帮助