Muhammad Tauseef
Muhammad Tauseef

Reputation: 433

ExpandableListView not showing child views

public class ItemOptionsListAdapter extends BaseExpandableListAdapter {

private Context context;
private LayoutInflater inflater;
private List<String> categoryList;

public ItemOptionsListAdapter(Context context, List<String> categoryList) {
    this.context = context;
    this.categoryList = categoryList;
    this.inflater = LayoutInflater.from(this.context);
}

private TextView title(View v, int resId, String text){
    TextView tv = (TextView) v.findViewById(resId);
    tv.setText(text);
    return tv;
}

private ImageView image(View v, int resId, int icon){
    ImageView iv = (ImageView) v.findViewById(resId);
    return iv;
}

class ItemListViewHolder{
    TextView title;
    ImageView dropdownArrowImage;
}

@Override
public int getGroupCount() {
    return categoryList.size();
}

@Override
public int getChildrenCount(int groupPosition) {
    return 1;
}

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

@Override
public Object getChild(int groupPosition, int childPosition) {
    return "size";
}

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

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

@Override
public boolean hasStableIds() {
    return false;
}

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

    ItemListViewHolder categoryItemViewHolder;
    if(convertView == null) { 
        convertView = inflater.inflate(R.layout.category_list_row_layout, null); 
        categoryItemViewHolder = new ItemListViewHolder(); 
        convertView.setTag(categoryItemViewHolder); 
    } 
    else { 
        categoryItemViewHolder = (ItemListViewHolder) convertView.getTag(); 
    }
    categoryItemViewHolder.title = title(convertView, R.id.category_name_text, categoryList.get(groupPosition));
    categoryItemViewHolder.dropdownArrowImage = image(convertView, R.id.dropdown_arrow_image, 0);
    return convertView;
}

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

    if(convertView == null) { 
        convertView = inflater.inflate(R.layout.item_option_list_layout, null);
    } 
    Log.d("child", " in getChildView()");
    TextView itemPropertyTextView = (TextView)convertView.findViewById(R.id.item_option_property);
    itemPropertyTextView.setText((String)"Size");
    return convertView;

}

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

My child views are going to be static and each group is meant to contain a single child childlayout:

<?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="horizontal" >

    <TextView android:id="@+id/item_option_property"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Size" />

    <RadioGroup android:id="@+id/item_option_radio_group"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

       <RadioButton android:text="Small"/>
       <RadioButton android:text="Medium"/>
       <RadioButton android:text="Large"/>

   </RadioGroup> 

</LinearLayout>

Group View layout:

<?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="horizontal"
android:background="#50ffffff" >

<TextView android:id="@+id/category_name_text"
    android:layout_width="0dp"
    android:layout_height="wrap_content" 
    android:layout_gravity="center_vertical"
    android:textSize="16sp" 
    android:layout_weight="1"
    android:layout_margin="15dp"
    android:textColor="#ffffff" />

<ImageView android:id="@+id/dropdown_arrow_image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:src="@drawable/ic_arrow_down"
    android:layout_margin="15dp"
    android:rotation="270" />

</LinearLayout>

Also from logs, I know that the getChildView never gets called

Thanks in advance

edit: I just noticed one thing, as soon as the groupView appears, a small scroll to its right flashes which gives an indication the all is working fine but the view itself isnt completely visible. But still dont seem to fix it.

My expandable list xml:

<ExpandableListView 
   android:id="@+id/item_option_list"
   android:layout_width="match_parent"
   android:layout_height="wrap_content" 
   android:groupIndicator="@null" >
</ExpandableListView>

Upvotes: 3

Views: 8902

Answers (7)

keerthy
keerthy

Reputation: 236

On going over the posted code, everything seems right. I would suggest trying the following to find out the issue.

1) In the layout inflate part add the following.

In getGroupView function :

convertView = inflater.inflate(R.layout.category_list_row_layout, parent, false);

In getChildView function :

convertView = inflater.inflate(R.layout.item_option_list_layout, parent, false);

The above lines of code will make sure it picks up the proper layout properties from its parent ViewGroup.

2) Add the log message Log.d("child", " in getChildView()"); in the beginning of the function which will let give us a clue as whether the function have not been called or there are any exception in rendering the view from the given layout.

3) Try constructing and returning a simple TextView from the getChildView() and then add complex views in incremental fashion.

Upvotes: 1

trojantale
trojantale

Reputation: 306

Make sure you've clicked the group header, 'cause the children are collapsed by default.

Also, to be sure not having a problem of focusability, try to expand programmatically.

expandableListView.expandGroup(0);

Upvotes: 5

Haris
Haris

Reputation: 1862

If you are using any Button or ImageButton with default properties will not work. see this for more details.

Upvotes: 0

Lusid
Lusid

Reputation: 21

I've had the same problem and I found my issue. The ExpandableListView's layout_height attribute was wrap_content which defaults to just the header. So you have 2 options to fix this.

Option 1 : Fix the layout_height to some value.

android:layout_height="100dp"

Option 2 : Programattically change the listview's height in onCreateView and wherever appropriate.

ViewGroup.LayoutParams params = expandableListView.getLayoutParams();
params.height = array_list.size() * 20;
expandableListView.setLayoutParams(params);
expandableListView.requestLayout();

Upvotes: 1

Distjoy
Distjoy

Reputation: 431

This is coming rather late, however for other readers...

The issue is caused by your

groupview linearlayout having horizontal orientation.

android:orientation="horizontal" -> set this to vertical instead.

Upvotes: 0

logan
logan

Reputation: 72

Use this code in the top layout of the group:

android:descendantFocusability="blocksDescendants"

Upvotes: 2

Huy H&#243;m Hỉnh
Huy H&#243;m Hỉnh

Reputation: 617

Try with another Group View layout. I also met this problem and I deleted some(I think not need) android: in the highest LinearLayout and It works. Hope this helps!

Upvotes: 0

Related Questions