ListFragment OnListItemClick not being called

后端 未结 11 1453
盖世英雄少女心
盖世英雄少女心 2020-11-27 05:53

I have a class that extends ListFragment, and it overrides the OnListItemClick method. I am also doing this in another ListFragment the same way (and the method gets called)

11条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-27 06:19

    Here is an elegant solution which allows onListItemClick to fire the way you think it should, and also allows any child views to receive events (or not, depending on how you want to set it.) For the question posed, this would allow for keeping the checkbox subview. Please take care to consider whether this is an intuitive experience for users.

    On the root ViewGroup of all list items, set the descendantFocusability attribute in XML:

    android:descendantFocusability="blocksDescendants"
    

    Android SDK documentation of descendantFocusability. Has been part of the SDK since Android 1.0. Other settings for android:descendantFocusability include "beforeDescendants" and "afterDescendants".

    Example cluster_layout.xml which sets descendantFocusability (List items applied to cluster_list.xml via an ArrayAdapter):

    
    
    
        
        
    
    

    Example cluster_list.xml, which has no bearing on this solution other than to show that it's intended for use in a ListFragment by having a ListView element with the id @id/android:list:

    
    
        
        
    
    

提交回复
热议问题