用于扩展列表视图中的组头和子布局的listSelector不能正常工作

时间:2015-02-20 05:45:21

标签: android selector expandablelistview

我正在使用ExpandableListview从服务器动态显示数据。

我为Group HeaderChild Layout使用了选择器,但我无法注意到任何反映的选择器。

请帮助我完成这项工作。

提前致谢。

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 个答案:

答案 0 :(得分:1)

您的选择器缺少默认选择器,如下所示放置默认颜色,而不是在ExpandableList中添加选择器,尝试将其添加为父和子布局的rootView的背景。

   
<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>