listSelector for Group Header and Child Layout in

2019-08-28 22:20发布

问题:

I am using ExpandableListview that displays data dynamically from server.

I used selector for Group Header and Child Layout, but I can't notice any selector that reflects.

Pls help me how to get this done.

Thanks in advance.

row_highlighter.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
   android:exitFadeDuration="@android:integer/config_mediumAnimTime">

   <item android:drawable="@android:color/holo_blue_bright" android:state_pressed="true"/>
   <item android:drawable="@android:color/holo_blue_light" android:state_selected="true"/>
   <item android:drawable="@android:color/holo_blue_dark" android:state_activated="true"/>
</selector>

ExpandableListviewDemo.java

expListView.setOnGroupClickListener(new OnGroupClickListener() {

            @Override
            public boolean onGroupClick(ExpandableListView parent, View v,int groupPosition, long id) {

                int index = parent.getFlatListPosition(ExpandableListView.getPackedPositionForGroup(groupPosition));
                            parent.setItemChecked(index, true);


                return false;
            }
        });



        // Clicking on Child        
        expListView.setOnChildClickListener(new OnChildClickListener() {            
            @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;
            }
        });

activity_demo.xml

<ExpandableListView
        android:id="@+id/search_expandable_listview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="@color/list_divider"
        android:childDivider="@drawable/listview_divider"
        android:dividerHeight="1dp"        
        android:listSelector="@drawable/row_highlighter"
        android:background="@color/list_background"
        android:layout_below="@+id/search_header"
        android:layout_above="@+id/layout_option_buttons"                      
        android:scrollingCache="false"
        android:transcriptMode="disabled"
        android:fastScrollEnabled="false"
        android:smoothScrollbar="true"
        android:fastScrollAlwaysVisible="false"
        android:animationCache="false"
        android:persistentDrawingCache="none"
        android:alwaysDrawnWithCache="false"
        android:drawingCacheQuality="low"/>

回答1:

Your selector is missing the default selector put the default color like below, instead of adding the selector in the ExpandableList try adding it as a background for the rootView of your Parent and Child layouts.

<selector xmlns:android="http://schemas.android.com/apk/res/android"
   android:exitFadeDuration="@android:integer/config_mediumAnimTime">

   <item android:drawable="@android:color/holo_blue_bright" android:state_pressed="true"/>
   <item android:drawable="@android:color/holo_blue_light" android:state_selected="true"/>
   <item android:drawable="@android:color/holo_blue_dark" android:state_activated="true"/>
   <item android:drawable="@android:color/transparent" />
  </selector>