适配器不会调用getChildView

时间:2013-12-18 15:54:11

标签: android expandablelistview android-adapter

我不得不承认我是Android的新手,而我的ExpandableListView给我带来了很多麻烦。我认为这是适配器无法正常工作。我正在使用扩展BaseExpandableListAdapter的类。

从记录中我知道的很多: 我交给适配器构造函数的列表正确填充。 getGroupCount返回1(是的,只有1个组),getChildrenCount返回18(如预期的那样)。方法getGroupView被调用(Log.d ...)但getChildView不是 - 不是在这个方法的最开始时记录而不是在调试时(没有达到断点)。 没有错误消息 - 只是ExpandableListView悄然没有扩展。

知道出了什么问题吗?我是否需要ViewHolder或者它是否正确地使群组膨胀?我完全迷失了......

我的部分代码:

package de.cimitery.android.cimitery;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

public class ExpandableAdapter extends BaseExpandableListAdapter {

    private List<GroupCat> catList;
    private Context ctx;
    NewGraveActivity app;

    public ExpandableAdapter(List<GroupCat> catList, Context ctx, NewGraveActivity app) {
        this.catList = catList;
        this.ctx = ctx;
        this.app = app;
    }

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

    @Override
    public int getGroupCount() {
        Log.d("ExAdapter getGroupCount", "" + catList.size());
        return catList.size();
    }

    @Override
    public long getGroupId(int groupPosition) {
        return catList.get(groupPosition).hashCode();
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {
        Log.d("ExAdapter getGroupView", "Start");
        View v = convertView;

        if (v == null) {
            LayoutInflater inflater = (LayoutInflater)ctx.getSystemService
                      (Context.LAYOUT_INFLATER_SERVICE);
            v = inflater.inflate(R.layout.group_layout, parent, false);
        }

        TextView groupName = (TextView) v.findViewById(R.id.groupName);

        GroupCat cat = catList.get(groupPosition);

        groupName.setText(cat.getGroupName());

        return v;
    }


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

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return catList.get(groupPosition).getItemList().get(childPosition).hashCode();
    }

    @Override
    public View getChildView(final int groupPosition, int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {
        Log.d("ExAdapter getChildView", "Start");
        View v = convertView;

        if (v == null) {
            LayoutInflater inflater = (LayoutInflater)ctx.getSystemService
                      (Context.LAYOUT_INFLATER_SERVICE);
            v = inflater.inflate(R.layout.item_layout, parent, false);
        }

        CheckBox itemCheck = (CheckBox) v.findViewById(R.id.itemCheck);
        TextView itemName = (TextView) v.findViewById(R.id.itemName);

        if (app.selected.containsKey((groupPosition))==true)               
            itemCheck.setChecked(true);
        else
            itemCheck.setChecked(false);


        Category child = catList.get(groupPosition).getItemList().get(childPosition);
        Log.d("ExAdapter getChildView", child.getName());

        itemName.setText(child.getName());

        itemCheck.setOnCheckedChangeListener(new CheckListener(childPosition, app));

        return v;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        int size = catList.get(groupPosition).getItemList().size();
        System.out.println("number of children for group ["+groupPosition+"] is ["+size+"]");
        return size;
    }

}

基的布局:

<?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="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/groupName"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingLeft="15dip" />

    <TextView
        android:id="@+id/groupDescr"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textSize="8dip" />

</LinearLayout>

项目布局:

<?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="match_parent"
    android:orientation="vertical" >

    <CheckBox
        android:id="@+id/itemCheck"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
    />

    <TextView
        android:id="@+id/itemName"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
         />
</LinearLayout>

使用exp布局列表视图:

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <ExpandableListView
            android:id="@+id/expandableListView1"
            android:layout_width="match_parent"
            android:layout_height="fill_parent"
            android:layout_marginTop="10dp" >
       </ExpandableListView>


           <Button
            android:id="@+id/buttonNewGrave"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/buttonNewGrave" />


    </LinearLayout>

</ScrollView>

1 个答案:

答案 0 :(得分:1)

第一件事: 您不应该在滚动视图中使用任何滚动组件(如可扩展列表),这是Listview inside ScrollView is not scrolling on Android的原因。

第二件事: ExpandableListView的高度应为match_parent。在您的情况下,您已在Linear Layout中使用Expandable ListView,其高度为wrap_content,将其更改为match_parent。

我遇到了同样的问题,我也按照同样的方法解决了这个问题。