Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
902 views
in Technique[技术] by (71.8m points)

android - Change ListView's text color on click

I have a customized ListView that each row of the list is composed by an ImageView and two TextView's. I want to change the text's color to white when a list's item is clicked (only in the clicked item). Also, I want to change back the color to black when the item is "unclicked" (when the "press" is released). I already made the item's background color change when it was clicked with the following list_item_bg.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/white" />

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

<item android:state_selected="true"
    android:drawable="@color/red_destaques" />
</selector>

And the listitem_row.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    android:id="@+id/relativeLayout1"
    android:layout_width="fill_parent"
    android:layout_height="50dip"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:padding="5dip"
    android:maxHeight="50dip"
    android:adjustViewBounds="true"
    android:background="@color/list_item_bg">

    <ImageView
        android:layout_width="70dip"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:adjustViewBounds="true"
        android:scaleType="fitStart" 
        android:src="@drawable/imagem_padrao_lista" 
        android:id="@+id/listItemLogo">
    </ImageView>

    <TextView
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:layout_width="wrap_content"
        android:layout_toRightOf="@+id/listItemLogo"
        android:layout_toLeftOf="@+id/listItemArrow"
        android:textColor="#000000"
        android:layout_marginLeft="5dip"
        android:adjustViewBounds="true" 
        android:id="@+id/listItemTitle">
    </TextView>

    <TextView
        android:layout_height="wrap_content"
        android:text="TextView"
        android:layout_width="wrap_content"
        android:layout_toRightOf="@+id/listItemLogo"
        android:layout_below="@+id/listItemTitle"
        android:textColor="#333333"
        android:layout_marginLeft="5dip"
        android:adjustViewBounds="true" 
        android:id="@+id/listItemDescription">
    </TextView>
    <ImageView android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_alignParentRight="true" android:src="@drawable/setinha_navegacao" android:layout_centerVertical="true" android:id="@+id/listItemArrow"></ImageView>

</RelativeLayout>

I the text to change it's color exactly in the same manner that the background changes as shown in the above xmls. I would prefer doing this change by code if possible...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Create a new StateListDrawable like you did before but with black as default color and white when pressed.

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:color="@color/black" />
<item android:state_focused="true" android:color="@color/black" />
<item android:state_pressed="true" android:color="@color/black" />
<item android:color="@color/white" />
</selector>

Now for in the TextView change the text color to the new drawable:

android:textColor="@color/list_item_text"

More on StateListDrawables: http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...