简体   繁体   中英

Change the view background when an item from a ListView is activated/selected

I'm going to explain (or at least try that) what I want to achieve because this is driving me crazy.

I have a ListView and the items have the following layout:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="10dp">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/item_background"
        android:duplicateParentState="true" >

        ... some contents here...

    </FrameLayout>

</FrameLayout>

"item_background.xml" contains a selector so the background changes depending on the item's state. It's working fine when I press and release the item but it's not working when I 'select' the item with a DPAD/Trackball. I have tried everything (state_activated, state_focused, state_selected...) but nothing works. I'm trying to search for a solution but looks like I can't find the proper keywords to search for.

Any ideas?

Thank you!

Finally I fixed it. The problem was in the order I put the items inside the selector (I put a state_pressed="false" before the state_selected="true").

Now it works flawlessly.

Thank you everyone for your help.

这很可能行不通,因为根布局上方的某些布局会获得焦点(如果没有其余布局,很难说清楚)。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM