Highlight for selected item in expandable list

后端 未结 11 2101
攒了一身酷
攒了一身酷 2021-01-31 21:25

I have a layout where I have an expandable list in a fragment on the left and a details fragment on the right. This all works fine.

Now I would like to indicate what

11条回答
  •  囚心锁ツ
    2021-01-31 22:02

    Do the following on ExpandableListView:

    Step 1. Set choice mode to single (Can be done in xml as android:choiceMode = "singleChoice")

    Step 2. Use a selector xml as background (android:listSelector = "@drawable/selector_list_item")

    
    
    
       
       
       
    
    
    

    Step 3. Call expandableListView.setItemChecked(index,true) in onChildClick() callback.

    index is a 0 based index of the child item calculated as follows

    Group 1 [index = 0]

    • Child item 1
    • Child item 2
    • Child item 3 [index = 3]

    Group 2 [index = 4]

    • Child item 1
    • Child item 2
    • Child item 3 [index = 7]

    Group 3 [index = 8]

    • Child item 1
    • Child item 2
    • Child item 3 [index = 11]

    If we have list headers as well, they would also account for index values.

    Here is a working example:

    @Override
    public boolean onChildClick(ExpandableListView parent, View v,
            int groupPosition, int childPosition, long id) {
        ...
    
        int index = parent.getFlatListPosition(ExpandableListView.getPackedPositionForChild(groupPosition, childPosition));
        parent.setItemChecked(index, true);
    
    
        return true;
    }
    

提交回复
热议问题