ExpandableListView指标图标取决于组状态

时间:2014-04-13 16:48:36

标签: android expandablelistview expandablelistadapter

我有一个带有ExpendableListView的Activity。我想展示不同的集团指标,取决于集团是否扩大以及该集团是否有子女:

  • 我有一个没有孩子的小组,因此我想使用android:drawable="@android:color/transparent"
  • 隐藏指标
  • 如果某个群组有一个或多个子女,则应显示此指标android:drawable="@drawable/forward,且仅当该群组未展开时
  • 如果某个群组已展开,我想显示此指标:android:drawable="@drawable/back

不幸的是,如果显示活动,则没有Childs的组和具有Childs的组没有图标。如果我展开群组(与孩子一起),则会显示图标android:drawable="@drawable/forward,而不是android:drawable="@drawable/back

不知何故,我的ExpandableListView状态出现问题。 * 有谁知道,Android如何确定群组的状态或我的问题是什么? *

这是我的代码:

这是我的activity.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <include layout="@layout/titlebar" />

    <ExpandableListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:groupIndicator="@drawable/group_indicator"  >
    </ExpandableListView>

</LinearLayout>

这是group_indicator.xml

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

    <item android:drawable="@drawable/forward" android:state_empty="false"/>
    <item android:drawable="@android:color/transparent" android:state_empty="true"/>
    <item android:drawable="@drawable/back" android:state_selected="true"/>

</selector>

这是我的ExpandableListAdapter.java:

public class ExpandableListAdapter extends BaseExpandableListAdapter {

    private final SparseArray<Group> groups;
    private LayoutInflater inflater;

    public ExpandableListAdapter(Activity act, SparseArray<Group> groups) {
        activity = act;
        this.groups = groups;  
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return groups.get(groupPosition).children.get(childPosition);
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public View getChildView(int groupPosition, final int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {
        final String children = (String) getChild(groupPosition, childPosition);
        TextView text = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.listrow_details, null);
        }

        text = (TextView) convertView.findViewById(R.id.copyrightTextView);
        text.setText(Html.fromHtml(children));

        return convertView;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return groups.get(groupPosition).children.size();
    }

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

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

    @Override
    public void onGroupCollapsed(int groupPosition) {
        super.onGroupCollapsed(groupPosition);
    }

    @Override
    public void onGroupExpanded(int groupPosition) {
        super.onGroupExpanded(groupPosition);
    }

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

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {

        boolean isLinearLayout = groups.get(groupPosition).isLinearLayout();

        if (convertView == null && isLinearLayout || isLinearLayout
                && !(convertView instanceof LinearLayout)) {
            convertView = inflater.inflate(groups.get(groupPosition)
                    .getLinearLayoutId(), null);

        }
        else if (convertView == null || !isLinearLayout
                && convertView instanceof LinearLayout) {
            convertView = inflater.inflate(R.layout.listrow_group, null);
        }

        if (!(convertView instanceof LinearLayout)) {
            Group group = (Group) getGroup(groupPosition);
            ((CheckedTextView) convertView).setText(group.string);
            ((CheckedTextView) convertView).setChecked(isExpanded);
        }
        return convertView;
    }

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

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return groups.get(groupPosition).children.size() > 0;
    }
}

1 个答案:

答案 0 :(得分:2)

这里有一些代码可以帮助您。我省略了部分,你需要包含你的代码。检查回答问题的getGroupView(...)代码。

public class ExpListAdapter extends BaseExpandableListAdapter {
private Context context;
private myServiceList = null;
public ExpListAdapter(Context cxt) {
    this.context = cxt;
    myServiceList = MyUtils.GetServiceList(context);
}
@Override
public Object getChild(int index, int childIndex) {
    return myServiceList.get(index);
}
@Override
public long getChildId(int index, int childIndex) {
    return childIndex;
}
@Override
public View getChildView(final int groupPosition, int childPosition,
        boolean isLastChild, View convertView, ViewGroup root) {
    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.expendable_list_child, null);
    }
    // your child view code goes here
    return convertView;
}
@Override
public int getChildrenCount(int arg0) {
    return 1;
    // I am keeping only on child in expandable list.
    // If you want you can make this dynamic too.
}
@Override
public Object getGroup(int i) {
    if (myServiceList != null || i < myServiceList.getSize()) {
        return myServiceList.get(i).getDisplayName();
    }
    return "NOT FOUND";
    // Some group name 
}
@Override
public int getGroupCount() {
    if (myServiceList == null) {
        return 0;
    }
    return myServiceList.getSize();
}
@Override
public long getGroupId(int i) {
    return i;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
        View convertView, ViewGroup root) {
    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if (convertView == null) {
        convertView = inflater.inflate(R.layout.expendable_item, root,
                false);
    }
    // your code goes here

    // This is what you asked for in your question
    ImageView imExpandableIndicator = (ImageView) convertView
            .findViewById(R.id.imv_expmain_indicator);
    if (isExpanded) {
        //set the indicator when expandable list is in expanded form
        imExpandableIndicator.setImageResource(R.drawable.uparrow);
    } else {
        //set the indicator when expandable list is not in expanded form
        imExpandableIndicator.setImageResource(R.drawable.downarrow);
    }
    // rest of your code if any
    return convertView;
}
@Override
public boolean hasStableIds() {
    // TODO Auto-generated method stub
    return false;
}
@Override
public boolean isChildSelectable(int arg0, int arg1) {
    // TODO Auto-generated method stub
    return true;
}
}