突出显示的ListView在Android上所选项目 [英] Highlight selected item in ListView on Android

查看:187
本文介绍了突出显示的ListView在Android上所选项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在做,随着Android的列表视图运行的应用程序,我不能让这个选择(chacked)项目有不同的背景。我使用CHOICE_MODE_SINGLE。这是我的code到目前为止的样子:

I have been making an application that works with ListViews in Android, and I can't make it so that the selected (chacked) item has a different background. I'm using CHOICE_MODE_SINGLE. This is how my code looks so far:

这是我使用的ListView控件:
(里面的 layout.xml

The ListView that I use:
(inside layout.xml)

<ListView
    android:id="@+id/listView"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:choiceMode="singleChoice"
    android:listSelector="@drawable/selector_test" >
</ListView>


TextView的布局我在适配器中使用:
listItem.xml


The TextView layout I use in the adapter:
(listItem.xml)

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/listItem"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="23sp"
    android:textStyle="bold" />


这是我添加适配器:


This is where I add the adapter:

mListAdapter = new ArrayAdapter<String>(this, R.layout.listItem, mList);
mListView = (ListView) findViewById(R.id.listView);
mListView.setAdapter(mAuthorAdapter);
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
        String selected = mList.get(position);
        // ...
        mListView.setItemChecked(position, true);
    }
});

我敢肯定,正确的项目被选中的点击,因为当我打电话getCheckedItemPosition(),它返回正确的值。

I'm sure that the proper item is checked on click, because when I call getCheckedItemPosition(), it returns the proper value.

而现在,两件事情我想以突出的检查项目:

And now, the two things I tried in order to highlight the checked item:

选择绘制:
selector_test.xml

Selector drawable:
(selector_test.xml)

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

    <item android:state_checked="true"><shape>
            <solid android:color="@color/red" />
        </shape></item>
    <item android:state_selected="true"><shape>
            <solid android:color="@color/blue" />
        </shape></item>
    <item android:state_pressed="true"><shape>
            <solid android:color="@color/green" />
        </shape></item>

</selector>

我把它添加到.XML有:

I add it to .xml with:

android:listSelector="@drawable/selector_test"


背景绘制:
background_test.xml


Background drawable:
(background_test.xml)

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

    <item android:drawable="@color/red" android:state_checked="true"/>
    <item android:drawable="@color/blue" android:state_selected="true"/>
    <item android:drawable="@color/green"/>

</selector>

我把它添加到.XML有:

I add it to .xml with:

android:background="@drawable/background_test"


我试着加入选择器和后台两个listView.xml和listItem.xml,但改变的是默认的背景色的唯一的事情,和选择的颜色,当该项目是pressed(或持有)。
机器人:state_checked =true和安卓state_selected =真似乎什么也不做

我可以通过覆盖在ArrayAdapter的getView()方法,如果选择视图调用setBackgroundColor()在它改变的背景下,它确实改变的背景下,同时也摆脱了选择的全部。另外,我真的不喜欢覆盖类只是改变一行code,尤其是如果同样的事情可以用不同的方式来实现。

那么,我问的是,有没有办法通过增加一个选择或背景绘制到它突出的ListView选中项,我只是做错了,否则我将不得不使其工作一些其他的方式。

在此先感谢! :)


I've tried adding the selector and the background to both listView.xml and listItem.xml, but the only thing that changes is the default background color, and the color of the selector when the item is pressed (or held).
android:state_checked="true" and android:state_selected="true" seem to do nothing.

I can change the background by overriding the getView() method in ArrayAdapter and invoking setBackgroundColor() in it if the view is selected, and it does change the background, but also gets rid of the selector entirely. Also, I don't really like to override classes just to change one line of code, especially if that same thing can be achieved in a different way.

So what I'm asking is, is there a way to highlight checked item in ListView by adding a selector or background drawable to it, and I am just doing it wrong, or will I have to make it work some other way.

Thanks in advance! :)

推荐答案

在您的活动ONSTART加入这一行

add this line in onStart of your activity

lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

其中,LV是ListView控件实例

where lv is instance of listView

然后重写此方法并添加以下行吧。

then override this method and add the following lines to it.

 @Override
public void onListItemClick(ListView l, View v, int position, long id) {


    // Set the item as checked to be highlighted 
    lv.setItemChecked(position, true);
            v.setBackgroundColor(Color.BLUE);

        conversationAdapter.notifyDataSetChanged();

}

,然后更改previous所选项目的背景回到正常的自定义适配器的getView方法颜色

and then change the color of previous selected item's background back to normal in getView method of your custom adapter

这篇关于突出显示的ListView在Android上所选项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆