我是 android 新手,我现在正在尝试使用 ExpandableListView 首先列出组级别的工作类型,然后列出子级别的工作项。但是很奇怪,布局 fill_parent 在 getChildView 方法中不起作用,它就像 wrap_content 一样,当我记录它们与我在布局中设置的内容相关时,相关的 textviews 的高度也变为 0,因此所有 textview 看起来都消失了,除非我手动设置文本大小在代码中。最奇怪的是只有子视图有问题,组级别工作正常,我不需要设置任何文本大小或高度。
下面是一些代码,你能帮我看看是怎么回事吗?非常非常感谢你!!
- 活动
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 } }
- 组级布局:work_items_h.xml //正常工作
<?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>
- 子级布局:work_items_c.xml //fill_parent 不起作用,textview 高度也不行
<?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>