How can ExpandableListView's default list item

2019-05-07 03:14发布

问题:

Is it possible to preserve ExpandableListView's default list item press behavior when supplying a group view that contains more than just a TextView. The behavior I'm referring to is where the item being pressed changes background color to yellow.

Every time I supply a group view that contains e.g. a TextView and a Button, I lose the behavior.

回答1:

Another option is setting your root ViewGroup to block focus for child views.

Example: <LinearLayout ... android:descendantFocusability="blocksDescendants" />



回答2:

Including any view that is focusable within a custom list item layout will cause the list item to stop responding to presses. When implementing this sort of custom view, the focusable property of each view within the list item view must be set to false. This can be done either in xml or code. With one exception - ImageButton will not respond to setting its focusable field via xml. It only works in code with ImageButton.

<TextView
    android:id="@+id/text1"
    android:focusable="false"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

Or

imageButtonInstance.setFocusable(false);