I'm new to android, I'm now trying to use ExpandableListView to list firstly work types in group level then work items in child level. But it's very strange that layout fill_parent not work in getChildView method, it just act like wrap_content, and also relevant textviews' height become 0 when I Log them regardness of what I set in layout and thus all textview looks gone unless I manually set text size in code. Most strangest thing is only child view has the problem, group level works fine where I do not need to set any textsize or height.
Below are some pieces of the code, could you please help me find out what's the matter? Thank you very very much!!
- Activity
public class WorkListActivity extends Activity implements ExpandableListView.OnGroupClickListener { private ExpandableListView elv; List<WorkTypeBean> worktypes = null; ExpandableAdapter viewAdapter; WorkTypeBean worktypeBean = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.work_items); elv = (ExpandableListView) findViewById(R.id.workList_ev); elv.setOnGroupClickListener(this); getWorkTypes(); //get all work types user can visit from server } @Override public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) { if (!parent.isGroupExpanded(groupPosition)){ getWorkItems(groupPosition); //fetch work items from server only when specific group clicked } return false; } class ExpandableAdapter extends BaseExpandableListAdapter { @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { ViewHolder holder; if (convertView==null){ LayoutInflater layoutInflater = (LayoutInflater) getBaseContext() .getSystemService(Context.LAYOUT_INFLATER_SERVICE); convertView = layoutInflater.inflate( R.layout.work_items_c, null); holder = new ViewHolder(); holder.tv1 = (TextView) convertView.findViewById(R.id.tv_work_item_title); holder.tv2 = (TextView) convertView.findViewById(R.id.tv_work_item_applier); holder.iv1 = (ImageView) convertView.findViewById(R.id.iv_work_item_hl); convertView.setTag(holder); }else{ holder = (ViewHolder)convertView.getTag(); } convertView.setBackgroundColor(Color.WHITE); WorkTypeBean typeBean = worktypes.get(groupPosition); if (typeBean==null){ return convertView; } WorkItemBean workitem = typeBean.getItem(childPosition); if (workitem==null){ return convertView; } String title = workitem.getTitle(); String applier = workitem.getApplier(); String postdate = workitem.getArriveTime(); int waitdays = Integer.parseInt(workitem.getWaitDays()); holder.tv1.setTextColor(Color.BLACK); holder.tv1.setText(title); holder.tv1.setTextSize(15); holder.tv2.setTextColor(Color.BLACK); holder.tv2.setTextSize(10); //if not set, tv2 remains original text "loading..." from work_item_c.xml holder.tv2.setText(applier+" ("+postdate+")"); if (waitdays > 2){ holder.iv1.setImageResource(R.drawable.wl_urgent); }else{ holder.iv1.setImageResource(R.drawable.wl_normal); } return convertView; } @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { ViewHolder holder; if (convertView == null) { LayoutInflater inflater = (LayoutInflater) getBaseContext() .getSystemService(LAYOUT_INFLATER_SERVICE); convertView = inflater.inflate(R.layout.work_items_h, null); holder = new ViewHolder(); holder.tv1 = (TextView) convertView.findViewById(R.id.tv_worktype); holder.iv1 = (ImageView) convertView.findViewById(R.id.iv_worktype_counter); convertView.setTag(holder); }else{ holder = (ViewHolder)convertView.getTag(); } WorkTypeBean worktype = worktypes.get(groupPosition); if (worktype==null){ return convertView; } holder.tv1.setTextColor(Color.BLACK); holder.tv1.setText(worktype.getTitle()); return convertView; } //... some other code } }
- group level layout: work_items_h.xml //works correctly
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="fill_parent" android:layout_height="match_parent" > <RelativeLayout android:layout_width="wrap_content" android:layout_height="match_parent" > <TextView android:id="@+id/tv_worktype" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="50dp" android:textAppearance="?android:attr/textAppearanceLarge" /> <ImageView android:id="@+id/iv_worktype_counter" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_alignParentTop="true" /> </RelativeLayout> </LinearLayout> </LinearLayout>
- child level layout: work_items_c.xml //fill_parent not work, neither textview height
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" > <ImageView android:id="@+id/iv_work_item_hl" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/wl_normal" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <TextView android:id="@+id/tv_work_item_title" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="loading..." android:textSize="15dp" /> <!--tried android:layout_height="fill_parent" but not work--> <TextView android:id="@+id/tv_work_item_applier" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="right" android:text="loading..." android:textAppearance="?android:attr/textAppearanceSmall" android:textSize="10dp" /> </LinearLayout> </LinearLayout>