如何突出显示ExpandableListView的组行

时间:2012-08-06 10:42:14

标签: java android listview android-layout expandablelistview

使用基于State List Selector的以下代码,当用户在显示屏上按下它时,我设法突出显示ExpandableListView的单个子行:

XML状态选择器代码:

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android" >

  <item 
      android:state_pressed="true"
      android:drawable="@color/highlighted_entry" /> 

  <item 
      android:state_selected="true"
      android:drawable="@color/highlighted_entry" />

  <item android:drawable="@color/transparent" />

</selector>

XML ExpandableListView布局代码:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout

    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dp"
    android:background="@drawable/exp_list_entry_selector" 
    android:layout_marginTop="5dp"
    android:layout_marginBottom="5dp" >

    <TextView android:id="@+id/exp_list_entry_title"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent"
       android:gravity="center_vertical"
       android:layout_alignParentTop="true"
       android:layout_alignParentBottom="true"
       android:textStyle="bold"
       android:textSize="12dp"
       android:textColor="#000000" 
    />

</LinearLayout>

Java代码:

@Override
public boolean onChildClick(    ExpandableListView parent, View v,
                                int groupPosition, int childPosition, long id ) 
{
    v.setSelected(true);    
    return false;
}

如果我尝试突出显示Group Row(可扩展的ListView条目),同样的方法将不起作用:当用户按下它时会突出显示(并展开或折叠它们),但释放后,背景颜色将恢复正常。

无论是否展开,我如何突出显示组行?

2 个答案:

答案 0 :(得分:3)

android:background="@drawable/row_highlighter"属性添加到child.xml的{​​{1}}。并创建expandableListView文件。

row_highlighter.xml

和源代码:

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

<item android:drawable="@android:color/holo_orange_dark" android:state_activated="true"/>
<item android:drawable="@android:color/transparent"/>

</selector>

Highlighting the selected item in the ExpandableListView

答案 1 :(得分:-1)

设置适配器时,请像这样传递自己的自定义布局

SimpleCursorTreeAdapter mAdapter = new ListinoProdottiAdapter(null, ListinoProdottiActivity.this,

                                R.layout.my_simple_expandable_list_item_1,
                                R.layout.listino_prodotti_row,
                                ....
                        );

R.layout.my_simple_expandable_list_item_1 (我将背景设为红色)

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    android:layout_width="match_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:gravity="center_vertical"
    android:background="@color/red"
/>