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 Expandable开发者_开发知识库ListView 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>
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,
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;
}
}
}
精彩评论