0

所以我正在使用以下内容,所以我可以更改我的列表视图项目的点击颜色。但是,我对单击后返回活动时发生的事情感到恼火-颜色与单击它大约一秒钟时保持相同,然后刷新为正常背景。此外,如果我按住 listview 项目并将手指滑开,则 listview 项目会保持不变,因为它被连续单击并且不会返回,直到我点击另一个 listview 项目。如何仅在点击时更改列表视图项目的背景颜色,而不是在切换回相同的活动后保持不变?这是我的代码。谢谢!

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background_repeatable"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".CategoriesActivity">

    <ListView
        android:id="@+id/categoriesList"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="@color/White"
        android:textAlignment="center"
        android:visibility="visible" 
        android:divider="@android:color/transparent"
        android:dividerHeight="10.0sp"
        android:drawSelectorOnTop="false"
        android:listSelector="@drawable/list_view_item_background_on_click">
    </ListView>
</RelativeLayout>



<?xml version="1.0" encoding="utf-8"?>

<!-- Layout for individual news entries in a list -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:background="@drawable/list_view_item_background"
    android:gravity="center_vertical"
    android:paddingBottom="8.0dip"
    android:paddingLeft="12.0dip"
    android:paddingRight="3.0dip"
    android:paddingTop="8.0dip" >

    <ImageView
        android:id="@+id/imgUrl"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:paddingLeft="2sp"
        android:src="@drawable/ic_menu_search_holo_light">
    </ImageView>

    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_toRightOf = "@+id/imgUrl"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:textColor="@color/White"
        android:textSize="20sp"
        android:textStyle="bold" />

</RelativeLayout>



<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >
    <corners android:radius="10dp" />

    <stroke
        android:width="0.5dp"
        android:color="@color/Teal" />
    <solid android:color="@color/Teal" />
</shape>
4

1 回答 1

1

Here seems to be a good tutorial But you want to use state drawables to change the color in different states such as focused, pressed, enabled, disabled

StateListDrawable Docs

Doing it this way, the color will change when you press on it but when you return the state won't be pressed so it will be the default color, which I believe is what you want.

于 2013-08-13T00:53:44.280 回答