1

Hi I am working on an application in which when I select on an listitem its background color should change and it should also show a vertical bar of single color at left side of listitem like it appears in youtube application.

The image is attached below: enter image description here

I know how to change background color on listview item click but how do I add the red line which is apprearing in youtube application. For this I have thought of adding a view in listview item xml which I would make visible on item click event.But in that case how would I hide the view from other listview items? Or is their any other easy way to implement this?

4

3 回答 3

3

尝试将此可绘制对象用于项目 state_pressed:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape>
            <solid android:color="@android:color/holo_red_dark" />
        </shape>
    </item>
    <item android:left="10dp">
        <shape>
            <solid android:color="@android:color/black" />
        </shape>
    </item>
</layer-list>
于 2013-08-10T20:15:03.293 回答
0

您应该实现一个背景选择器。为此,请为不同的行状态(选中、聚焦等)创建一个具有不同可绘制对象的 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="@drawable/pressed"/> <!-- pressed -->
    <item android:state_focused="true" android:drawable="@drawable/focused" /> <!-- focused -->
    <item android:drawable="@drawable/default"/> <!-- default -->
</selector>

之后,只需将行背景 Drawable 设置为此文件。

于 2013-07-10T05:26:36.443 回答
0

要创建这种类型的列表视图,您需要创建自定义适配器。创建一个包含所有元素的视图。使用自定义 ArrayListAdapter 填充列表并使用背景选择器作为背景颜色。

此外,对于垂直条默认视图将是不可见的,并且对选择可见。创建一个公共静态列表以跟踪选择,即使在视图关闭后也是如此。

于 2013-07-10T05:33:39.157 回答