【问题标题】:Java android listView After clicking, the selection disappearsjava android listView 点击后选择消失
【发布时间】:2017-08-28 16:38:42
【问题描述】:

我想这样做:我有一个列表,其中每两个项目的颜色都不同,并且想要选择被点击的项目。选中的项目高亮显示,但释放后返回旧颜色

我这样做了: Artist_list_background_alternate.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/sign_out_color" />

 <item android:state_pressed="true"
     android:drawable="@color/survey_toplist_item" />

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

</selector> 

artists_list_backgroundcolor.xml:

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

Java 文件代码::

 @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        if (view == null) {
            view = lInflater.inflate(R.layout.listitem, parent, false);
        }

        if (position % 2 == 0) {
            view.setBackgroundResource(R.drawable.artists_list_backgroundcolor);
        } else {
            view.setBackgroundResource(R.drawable.artists_list_background_alternate);
        }

        ((TextView) view.findViewById(R.id.heading)).setText(data.get(position));

        return view;
    }


  ListView lvMain = (ListView) findViewById(R.id.list);
        lvMain.setAdapter(adapter);

布局xml ::

 <ListView
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:cacheColorHint="#00000000"
        android:dividerHeight="1dp" />

我不知道我做错了什么

【问题讨论】:

    标签: java android listview


    【解决方案1】:

    为了突出显示某个项目,您需要为您的ListView 提供choiceMode。例如:

    android:choiceMode="singleChoice"
    

    另外,在选择器中为状态 android:state_activated="true" 添加颜色。

    【讨论】:

    • 我添加了这个,但是当我停止推送一个项目时,项目有这个颜色;灰色,不是这个survey_toplist_item:
    • 你需要修复你的选择器。也介绍一下android:state_activated="true"。欲了解更多信息,read this
    • 我解决了这个问题,但是当我快速点击时我有延迟,有时我有两个或选定的项目
    猜你喜欢
    • 2016-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-09
    • 2016-03-12
    • 2019-09-30
    • 1970-01-01
    相关资源
    最近更新 更多