ExpandableListView不会附加子项

时间:2011-10-13 13:21:13

标签: android expandablelistview expandablelistadapter

我有一个包含电影信息的ExpandableListView。这些组是电影标题,孩子是与这部电影相关的媒体(都包含在电影对象中)。 显示ExpandableListView但仅显示组,当我单击它们时,列表不会展开。我调试了它,我发现getChildView从未执行过。 我正在搜索,但我无法意识到为什么这个方法没有被执行。

ArrayList<MovieMock> movies;
@Override
public void onCreate(Bundle savedInstanceState){    
    super.onCreate(savedInstanceState);

    setContentView(R.layout.savedsyncs_content);

    ExpandableListView l = (ExpandableListView) findViewById(R.id.ExpandableList_Content);

    laodMockedList();

    ContentExpandableListAdapter adapter = new ContentExpandableListAdapter(this, movies);
    l.setAdapter(adapter);
}

public class ContentExpandableListAdapter extends BaseExpandableListAdapter {
private ArrayList<MovieMock> movies;    
private Context context;

/*ExpandableList containing the movies from SavedSyncs. The groups are the movie's titles, the childs are 
 * the movie media and information.
 * */
public ContentExpandableListAdapter(Context context, ArrayList<MovieMock> movies){
    this.context = context;
    this.movies = movies;
}

@Override
public boolean areAllItemsEnabled()
{
    return true;
}

@Override
public MovieMock getChild(int groupPosition, int childPosition) {
    // TODO Auto-generated method stub
    Log.d("CHILD", "getChild : " + groupPosition);
    return movies.get(groupPosition);
}

@Override
public long getChildId(int arg0, int arg1) {
    // TODO Auto-generated method stub
    //return Long.parseLong(movies.get(arg0).getId());
    return arg1;
}

@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild,View convertView, ViewGroup parent) {
    MovieMock child = (MovieMock)getChild(groupPosition, childPosition);
    Log.d("DEBUG", "Entro al hijo") ;
    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.expandablelistcontent_child, null);
    }

    TextView audiotxt = (TextView) convertView.findViewById(R.id.audio_text);
    audiotxt.setText(child.getAudio());

    /*TextView videotxt = (TextView) convertView.findViewById(R.id.video_text);
    videotxt.setText(child.getAudio());*/

    return convertView;
}

@Override
public int getChildrenCount(int groupPosition) {
    // TODO Auto-generated method stub
    return 3;
}

@Override
public String getGroup(int groupPosition) {
    // TODO Auto-generated method stub
    return movies.get(groupPosition).getTitle();
}

@Override
public int getGroupCount() {
    // TODO Auto-generated method stub
    return movies.size();
}

@Override
public long getGroupId(int groupPosition) {
    // TODO Auto-generated method stub
    //return Long.parseLong(movies.get(groupPosition).getId());
    return groupPosition;
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded,
        View convertView, ViewGroup parent) {
    String movieTitle = (String) getGroup(groupPosition);

    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.expandablelistcontent_group, null);
    }

    TextView grouptxt = (TextView) convertView.findViewById(R.id.ContentText);

    grouptxt.setText(movieTitle);

    return convertView;
}

@Override
public boolean hasStableIds() {
    // TODO Auto-generated method stub
    return false;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
    // TODO Auto-generated method stub
    return true;
}

}

这是XML代码:

集团:

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
    android:id="@+id/ContentText" 
    android:text="Content"
    android:layout_width="wrap_content"
    android:layout_height="40dip"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true"/>
<ImageButton 
    android:id="@+id/delete_button" 
    android:layout_width="20dip" 
    android:layout_height="20dip" 
    android:background="@drawable/delete_button"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true"
    >
</ImageButton>

</RelativeLayout>

孩子:

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/layout_audio">
    <ImageView 
        android:background="@drawable/audio_icon" 
        android:layout_height="20dip" 
        android:layout_width="20dip" 
        android:id="@+id/audio_icon"
        >
    </ImageView>
    <TextView
        android:id="@+id/audio_text" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Audio"
        android:layout_toRightOf="@id/audio_icon"
    >
    </TextView>
</LinearLayout>

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/layout_video"
android:layout_below="@id/layout_audio">    
<ImageView 
    android:background="@drawable/video_icon" 
    android:layout_height="20dip" 
    android:layout_width="20dip" 
    android:id="@+id/video_icon">
</ImageView>
<TextView
    android:id="@+id/video_text" 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Trailer"
    android:layout_toRightOf="@id/video_icon"
>
</TextView>
</LinearLayout>
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/layout_image"
android:layout_below="@id/layout_video">
<ImageView 
    android:background="@drawable/image_icon" 
    android:layout_height="20dip" 
    android:layout_width="20dip" 
    android:id="@+id/image_icon">
</ImageView>
<TextView
    android:id="@+id/image_text" 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:text="Poster"
    android:layout_toRightOf="@id/image_icon"
>
</TextView>
</LinearLayout>
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:id="@+id/layout_info"
android:layout_below="@id/layout_image">  
<ImageView 
    android:background="@drawable/info_icon" 
    android:layout_height="20dip" 
    android:layout_width="20dip" 
    android:id="@+id/info_icon">
</ImageView>
<TextView
    android:id="@+id/info_text" 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Movie Information"
    android:layout_toRightOf="@id/info_icon"
>
</TextView>
</LinearLayout>
</RelativeLayout>

2 个答案:

答案 0 :(得分:3)

对于记录,问题是组布局中的ImageButton。我不知道为什么,但删除它我解决了问题。

答案 1 :(得分:2)

结帐我的示例,

public class MainExpand extends ExpandableListActivity {

    List<String> list = new ArrayList<String>();
    private ExpandableListAdapter adapter;
    private List<String> parentList = new ArrayList<String>();
    private List<List<String>> childList =new ArrayList<List<String>>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

         for (int i = 0; i < 2; i++) {
            parentList.add("parent "+i);
            list = new ArrayList<String>();
            for (int j = 0; j < 3; j++) {
                list.add("child "+j);
              }
            childList.add(list);
          }
        adapter = new MyExpandableListAdapter(this,parentList,childList);
        setListAdapter(adapter);
     }

      public class MyExpandableListAdapter extends BaseExpandableListAdapter {

            Activity mActivity;
            List<String> parentList;
            List<List<String>> childList;
            LayoutInflater inflater;

            public MyExpandableListAdapter(Activity mActivity,List<String> parent,List<List<String>> child)
            {
                this.mActivity =  mActivity;
                this.parentList = parent;
                this.childList = child;
                inflater = LayoutInflater.from(mActivity);
            }

            public Object getChild(int groupPosition, int childPosition) {
                return childList.get(groupPosition).get(childPosition);
            }

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

            public int getChildrenCount(int groupPosition) {
                return childList.get(groupPosition).size();
            }

            public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
                    View convertView, ViewGroup parent) {

                TextView txtview;
                if(convertView == null){

                    convertView = inflater.inflate(R.layout.main, null);
                    txtview = (TextView)convertView.findViewById(R.id.txtGroup);
                    convertView.setTag(txtview);
                }
                else{
                    txtview = (TextView) convertView.getTag();
                }
                txtview.setText(getChild(groupPosition, childPosition).toString());

                return convertView;
            }

            public Object getGroup(int groupPosition) {
                return parentList.get(groupPosition);
            }

            public int getGroupCount() {
                return parentList.size();
            }

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

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

                TextView txtview;
                if(convertView == null){

                    convertView = inflater.inflate(R.layout.main, null);
                    txtview = (TextView)convertView.findViewById(R.id.txtGroup);
                    convertView.setTag(txtview);
                }
                else{
                    txtview = (TextView) convertView.getTag();
                }
                txtview.setText(getGroup(groupPosition).toString());
                return convertView;
            }

            public boolean isChildSelectable(int groupPosition, int childPosition) {
                return true;
            }

            public boolean hasStableIds() {
                return true;
            }
        }
}