Android 安卓列表视图。如何更改手动选择的项目的背景颜色

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

Android ListView. How to change background color of manually selected item

androidlistview

提问by bukka.wh

Can You help me please. I need to change background color of my list view item which is selected manually by setSelection(int pos)function and I need to stay with new color until new setSelection call. I have read some topics of how to do this, but I still have no succes. Thanks!

你能帮我吗。我需要更改由setSelection(int pos)函数手动选择的列表视图项的背景颜色,并且我需要保持新颜色,直到新的 setSelection 调用。我已经阅读了一些关于如何做到这一点的主题,但我仍然没有成功。谢谢!

回答by T.V.

I've managed to accomplish that by making several selectors for the different states

我已经通过为不同的状态制作几个选择器来实现这一点

first put this in your listview

首先把它放在你的列表视图中

android:listSelector="@drawable/list_selector"

Then create xml files in drawable to control the diferent states

然后在drawable中创建xml文件来控制不同的状态

@drawable/list_selector

@drawable/list_selector

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/list_item_bg_normal" android:state_activated="false"/>
<item android:drawable="@drawable/list_item_bg_pressed" android:state_pressed="true"/>
<item android:drawable="@drawable/list_item_bg_pressed" android:state_activated="true"/>
</selector>

@drawable/list_item_bg_normal

@drawable/list_item_bg_normal

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
  android:startColor="@color/list_background"
  android:endColor="@color/list_background"
  android:angle="90" />
</shape>

@drawable/list_item_bg_pressed

@drawable/list_item_bg_pressed

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
  <gradient
      android:startColor="@color/list_background_pressed"
      android:endColor="@color/list_background_pressed"
      android:angle="90" />
</shape>

In your ListView Selection

在您的 ListView 选择中

listView.setOnItemClickListener(new OnItemClickListener() {

         @Override
         public void onItemClick(AdapterView<?> parent, View view, int position,long arg3) {
             view.setSelected(true);
             ...
         }
    }

Don't forget to add list_background_pressedand list_backgroundto your values/color.xml or just set the color manually in each file.

不要忘记将list_background_pressedlist_background添加到您的 values/color.xml 或只是在每个文件中手动设置颜色。

And I Believe that when you use setSelection(int pos)that will automatically uset the layout you've set as selectected.

而且我相信,当您使用setSelection(int pos) 时,它会自动使用您设置为已选择的布局。

Hope it helps.

希望能帮助到你。

回答by nandish

listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (updateview != null) updateview.setBackgroundColor(Color.TRANSPARENT);
        updateview = view;
        view.setBackgroundColor(Color.CYAN);
    }
});