子位置可扩展列表视图返回错误的值?

时间:2014-03-05 01:31:53

标签: android expandablelistview baseadapter

我正在尝试使用具有3种不同布局的ExpandabelistView。

结构如下: 如果是第一个布局,则要加载的视图应为firstview.xml 如果它是一个偶数孩子应该加载偶数视图,如果是奇数应该加载另一个。

问题在于它有时会起作用,但有时却不起作用。我试图记录childPosition,但每次都会得到不同的值。子位置值在同一个孩子中返回0。

这是我的getChildView()

的代码
@Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View     convertView, ViewGroup parent) {

    final String children = (String) getChild(groupPosition, childPosition);

    TextView textTitle;
    TextView textDescription;

    if(convertView == null) {
        if(childPosition == 0) {
            convertView = inflater.inflate(R.layout.listrow_first_element, null);
        } else if(childPosition % 2 != 0) {
            convertView = inflater.inflate(R.layout.listrow_details, null);
        } else {
            convertView = inflater.inflate(R.layout.listrow_details_color2, null);
        }
    }

    textTitle = (TextView) convertView.findViewById(R.id.titleView);
    textDescription = (TextView) convertView.findViewById(R.id.descriptionView);
    textTitle.setText(getChildString(childPosition));
    textDescription.setText(children);

    return convertView;
}

以下是儿童职位返回给我的Logcat:

    03-04 19:26:10.181  23283-23283/com.gabilheri.insuringmylife D/Child Position:﹕ 0
    03-04 19:26:10.181  23283-23283/com.gabilheri.insuringmylife D/Child Position:﹕ 1
    03-04 19:26:10.181  23283-23283/com.gabilheri.insuringmylife D/Child Position:﹕ 2
    03-04 19:26:10.191  23283-23283/com.gabilheri.insuringmylife D/Child Position:﹕ 3
    03-04 19:26:10.191  23283-23283/com.gabilheri.insuringmylife D/Child Position:﹕ 4
    03-04 19:26:10.191  23283-23283/com.gabilheri.insuringmylife D/Child Position:﹕ 5
    03-04 19:26:10.191  23283-23283/com.gabilheri.insuringmylife D/Child Position:﹕ 6
    03-04 19:26:10.201  23283-23283/com.gabilheri.insuringmylife D/Child Position:﹕ 7
    03-04 19:26:10.201  23283-23283/com.gabilheri.insuringmylife D/Child Position:﹕ 0
    03-04 19:26:10.211  23283-23283/com.gabilheri.insuringmylife D/Child Position:﹕ 1
    03-04 19:26:10.211  23283-23283/com.gabilheri.insuringmylife D/Child Position:﹕ 2
    03-04 19:26:10.221  23283-23283/com.gabilheri.insuringmylife D/Child Position:﹕ 3
    03-04 19:26:10.221  23283-23283/com.gabilheri.insuringmylife D/Child Position:﹕ 4
    03-04 19:26:10.221  23283-23283/com.gabilheri.insuringmylife D/Child Position:﹕ 5
    03-04 19:26:14.876  23283-23283/com.gabilheri.insuringmylife D/dalvikvm﹕ [GC Control] DisableGcInRange: toleranceRange = 4
    03-04 19:26:14.876  23283-23283/com.gabilheri.insuringmylife D/dalvikvm﹕ [GC Control] disableGcInRange: Left 4194304 bytes to trigger GC (current size:32409816 bytes)
    03-04 19:26:15.096  23283-23283/com.gabilheri.insuringmylife D/Child Position:﹕ 6
    03-04 19:26:16.207  23283-23283/com.gabilheri.insuringmylife D/dalvikvm﹕ [GC Control] nativeRestoreGc
    03-04 19:26:19.601  23283-23283/com.gabilheri.insuringmylife D/dalvikvm﹕ [GC Control] DisableGcInRange: toleranceRange = 4
    03-04 19:26:19.601  23283-23283/com.gabilheri.insuringmylife D/dalvikvm﹕ [GC Control] disableGcInRange: Left 4194304 bytes to trigger GC (current size:32414792 bytes)
    03-04 19:26:19.661  23283-23283/com.gabilheri.insuringmylife D/Child Position:﹕ 7
    03-04 19:26:20.001  23283-23283/com.gabilheri.insuringmylife D/Child Position:﹕ 1
    03-04 19:26:20.121  23283-23283/com.gabilheri.insuringmylife D/Child Position:﹕ 0
    03-04 19:26:20.952  23283-23283/com.gabilheri.insuringmylife D/dalvikvm﹕ [GC Control] nativeRestoreGc
    03-04 19:26:21.032  23283-23283/com.gabilheri.insuringmylife D/dalvikvm﹕ [GC Control] DisableGcInRange: toleranceRange = 4
    03-04 19:26:21.032  23283-23283/com.gabilheri.insuringmylife D/dalvikvm﹕ [GC Control] disableGcInRange: Left 4194304 bytes to trigger GC (current size:32416096 bytes)
    03-04 19:26:21.152  23283-23283/com.gabilheri.insuringmylife D/dalvikvm﹕ [GC Control] nativeRestoreGc
    03-04 19:26:23.114  23283-23283/com.gabilheri.insuringmylife D/dalvikvm﹕ [GC Control] DisableGcInRange: toleranceRange = 4
    03-04 19:26:23.114  23283-23283/com.gabilheri.insuringmylife D/dalvikvm﹕ [GC Control] disableGcInRange: Left 4194304 bytes to trigger GC (current size:32416096 bytes)
    03-04 19:26:23.154  23283-23283/com.gabilheri.insuringmylife D/Child Position:﹕ 6
    03-04 19:26:23.214  23283-23283/com.gabilheri.insuringmylife D/Child Position:﹕ 7
    03-04 19:26:23.555  23283-23283/com.gabilheri.insuringmylife D/Child Position:﹕ 0
    03-04 19:26:23.645  23283-23283/com.gabilheri.insuringmylife D/dalvikvm﹕ [GC Control] nativeRestoreGc

1 个答案:

答案 0 :(得分:2)

这是你的问题:

if(convertView == null) {
    if(childPosition == 0) {
        convertView = inflater.inflate(R.layout.listrow_first_element, null);
    } else if(childPosition % 2 != 0) {
        convertView = inflater.inflate(R.layout.listrow_details, null);
    } else {
        convertView = inflater.inflate(R.layout.listrow_details_color2, null);
    }
}

在调用getView()时,convertView不会始终为null,并且不会始终相同。 ListView的适配器将重用View,因为它认为合适,并且它无法区分您的视图类型。

您的解决方案是:

一个。每次都增加视图(比常规类型更昂贵)

B中。预膨胀视图然后将它们保存在支持适配器的对象中,并在调用getView()时返回它们(可能或多或少花费A型)

℃。不要做你想做的事(但这是一个愚蠢的选择,所以忘了这个)

除非你看到严重的性能问题,否则我可能会选择A。