简体   繁体   English

listView项目在单击后保持突出显示

[英]listView items to stay highlighted after being clicked

I have a ListView that I want the clicked item to have a background color to indicate which item is currently selected. 我有一个ListView ,我希望单击的项目具有背景颜色以指示当前选中的项目。 I have achieved that by specifying a selector field 我通过指定选择器字段实现了这一点

<ListView
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:id="@+id/sub_arguments_listView"
    android:layout_weight="1"
    android:listSelector="@drawable/list_selector"
    android:choiceMode="singleChoice"
    android:clickable="true"/>

And the list_selector.xml is: list_selector.xml是:

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

I have added a ListView.onItemClickListener() : 我添加了一个ListView.onItemClickListener()

 public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
   String text ="sometext";
    ((TextView)mMainView.findViewById(R.id.sub_argument_text)).setText(text);
}

This works fine and as expected as long as the variable "text" has a short contents. 只要变量“ text”的内容简短,此方法就可以正常工作。 The problem I am having is when the contents of the variable "text" is a long text then the TextView is updated fine but the selected item in the ListView will loose the background (back to transparent) for some reason (my guess is the focus is lost). 我遇到的问题是,当变量“文本”的内容为长文本时, TextView可以很好地更新,但是出于某种原因(我的猜测是焦点), ListView的选定项将使背景松散(变回透明)丢失)。 If I click on the same item again then the background stays and the item is still highlighted. 如果再次单击同一项目,则背景保持不变,并且该项目仍突出显示。 Also please note that I have two Android devices: 另外请注意,我有两个Android设备:

  1. A Marshmallow 5.0 which is a fast device. 棉花糖5.0是一种快速的设备。 And I don't see the issue. 而且我没有看到这个问题。
  2. The second is a 4.2 and relatively slow device which shows this issue. 第二个是4.2且相对较慢的设备,它显示了此问题。

What am I missing ? 我想念什么? How do I made the clicked item in the listView to remain highlighted all the time until another item is clicked ? 如何使listView中的被单击项始终保持突出显示状态,直到单击另一个项?

Thanks 谢谢

Try this, 尝试这个,

 public void onItemClick(AdapterView<?> parent, View view, int position, long id)
     {

       listView.setItemChecked(position, true);

     }

This may helps you. 这可能对您有帮助。

You can try this Example... 您可以尝试此示例...

Android ListView. Android ListView。 How to change background color of manually selected item 如何更改手动选择项目的背景颜色

also see this answer also 也看到这个答案

Change background color of selected item on a ListView 更改ListView上所选项目的背景颜色

In your code 在你的代码中

listView.setOnItemClickListener(new OnItemClickListener() {

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM