Pergunta

I have a custom expandable list view made of two custom objects. I want to have a click effect on the list's child items so the user knows which item they are clicking. The issue is, after creating the custom adapter I cannot get child on click effect. I have a selector in my drawable folder and I am assigning it to my Expandable list view like this and for some reason it is not working. Can anyone help me figure this out. Thanks

main.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.grr.MainActivity"
    tools:ignore="MergeRootFrame" >

    <ExpandableListView
        android:id="@+id/expandableListView1"
        android:choiceMode="singleChoice"
        android:layout_width="match_parent"
        android:listSelector="@drawable/list_selector"
        android:layout_height="wrap_content" >
    </ExpandableListView>

</FrameLayout>

and my selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@android:color/black"
        android:state_pressed="true" />
    <item android:drawable="@android:color/darker_gray"
        android:state_checked="true" />
    <item android:drawable="@android:color/white"
        android:state_selected="true" />
</selector>

and this is my CustomAdapter class

public class ExpandableListAdapter extends BaseExpandableListAdapter {

private Context _context;
private List<MainBodyArea> _listDataHeader; 
private HashMap<MainBodyArea, List<SubBodyArea>> _listDataChild;

public ExpandableListAdapter(Context context, List<MainBodyArea> listDataHeader,
        HashMap<MainBodyArea, List<SubBodyArea>> listChildData) {
    this._context = context;
    this._listDataHeader = listDataHeader;
    this._listDataChild = listChildData;
}

@Override
public Object getChild(int groupPosition, int childPosititon) {
    return this._listDataChild.get(this._listDataHeader.get(groupPosition))
            .get(childPosititon);
}

@Override
public long getChildId(int groupPosition, int childPosition) {
    SubBodyArea temp = this._listDataChild.get(this._listDataHeader.get(groupPosition)).get(childPosition);
    return temp.getID();
}

@Override
public View getChildView(int groupPosition, final int childPosition,
        boolean isLastChild, View convertView, ViewGroup parent) {
    SubBodyArea temp = (SubBodyArea) getChild(groupPosition, childPosition);
    final String childText = temp.getName();

    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) this._context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.list_child, null);
    }

    TextView txtListChild = (TextView) convertView
            .findViewById(R.id.lblListItem);

    txtListChild.setText(childText);
    return convertView;
}

@Override
public int getChildrenCount(int groupPosition) {
    return this._listDataChild.get(this._listDataHeader.get(groupPosition))
            .size();
}

@Override
public Object getGroup(int groupPosition) {
    return this._listDataHeader.get(groupPosition);
}

@Override
public int getGroupCount() {
    return this._listDataHeader.size();
}

@Override
public long getGroupId(int groupPosition) {
    return groupPosition;
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded,
        View convertView, ViewGroup parent) {
    MainBodyArea temp = (MainBodyArea) getGroup(groupPosition);
    String headerTitle = temp.getName();
    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) this._context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.list_header, null);
    }

    TextView lblListHeader = (TextView) convertView
            .findViewById(R.id.lblListHeader);
    lblListHeader.setTypeface(null, Typeface.BOLD);
    lblListHeader.setText(headerTitle);

    return convertView;
}

@Override
public boolean hasStableIds() {
    return false;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
    return true;
}
}
Foi útil?

Solução

It's very easy
In your list_child :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/selector"
    android:orientation="vertical">

<!-- your rest of layout -->
.............
.............
<!-- your rest of layout -->
</LinearLayout>
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top