开发者

GridView children don't display focus

In an android app, I have a GridView holding TextView created by an adapter. In those TextView I add an icon which has three states (pressed, selected and default.) I removed the default selector of the GridView with android:listSelector="#00000000" and I would like the selected state of the icon to display instead. But although the pressed state works (ie when the TextView is pressed, the pressed version of the icons is shown) the seleted doesn't.

I've tried those tricks (found at different places on the web) but it didn't work either:

  • setting

    android:descendantFocusability="afterDescendants"
    
  • or

    android:drawSelectorOnTop="true"
    
  • or (in the TextViews)

    android:duplicateParentState="true" />
    

And if I set the TextView to be focusable, it gets focus independently of the GridView (ie, clicking on it doesn't call t开发者_如何转开发he GridView onClick method...)

The icon is defined in a xml file like this:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:state_focused="true"
    android:state_pressed="true"
    android:drawable="@drawable/ic_flag_bg_pressed"/>
  <item android:state_focused="false"
    android:state_pressed="true"
    android:drawable="@drawable/ic_flag_bg_pressed"/>
  <item android:state_focused="true"
    android:drawable="@drawable/ic_flag_bg_selected"/>
  <item android:state_focused="false"
    android:state_pressed="false"
    android:drawable="@drawable/ic_flag_bg_default"/>
</selector>

Is there a way to tell the GridView to pass the focused state to its children ?


Got stuck on this as well. An alternate approach that can look okay is to use a border style selector and draw it on top.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜