Saturday, 24 August 2013

Android ExpandableListView with GridView

Step are: 1
Crete 3 xml file  a) list_items_column.xml

android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<RelativeLayout
android:id="@+id/rl_row_layout_header"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/tv_heading"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="4dp"
android:background="#E5EAF5"
android:gravity="center_vertical"
android:textStyle="bold" >

b) Expandable child is row_grid.xml


android:id="@+id/gridView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:columnWidth="80dp"
android:gravity="center"
android:numColumns="auto_fit"
android:stretchMode="columnWidth" >

3.Now the Last xml is grid childern.xml

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp" >
<TextView
android:id="@+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Test"
android:layout_marginTop="15sp"
android:textSize="15sp" >


2 Step... In expandable list Adapter get Child View U can add following code.....
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item_grid_row, null);
}
GridView label = (GridView) convertView.findViewById(R.id.gridView);
int totalHeight=0;
for (int size = 0; size < adapter.getCount(); size++) {
RelativeLayout relativeLayout = (RelativeLayout) adapter.getView(size, null,
label);
TextView textView = (TextView) relativeLayout.getChildAt(0);
textView.measure(0, 0);
totalHeight += textView.getMeasuredHeight();
}
ViewGroup.LayoutParams params = (ViewGroup.LayoutParams)label.getLayoutParams();
if (params != null)
{
params.height = totalHeight;
}
return convertView;
}