ExpandableListView 不追加子项
我有一个包含电影信息的 ExpandableListView。组是电影标题,子组是与该电影相关的媒体(全部包含在 Movie 对象中)。 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>
I have an ExpandableListView containing movie information. The groups are the movie titles, the childs are the media related to this movie (all included in the Movie object).
The ExpandableListView is showed but only with the groups, and when I click on them, the list does not expand. I debugged it and i figure out the getChildView is never executed.
I was searching but i cant realize why this method is not being executed.
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;
}
}
And here is the XML code:
The Group:
<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>
The child:
<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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据记录,问题出在组布局中的 ImageButton。我不知道为什么,但删除它我解决了问题。
For the records, the problem was the ImageButton in the group layout. I dont know why, but removing it i solved the problem.
查看我的示例,
Checkout my Example,