expandablelistview getchildview显示不正确
问题描述:
我是新来的android,我现在试图使用ExpandableListView首先列出组级别的工作类型,然后在子级别工作项目。但是很奇怪的是,布局fill_parent在getChildView方法中不起作用,它只是像wrap_content一样工作,并且当我将它们关注于布局中的设置时,相关textviews的高度变为0,因此除非手动设置文本大小在代码中。最奇怪的是只有子视图有问题,组级别工作正常,我不需要设置任何文本大小或高度。expandablelistview getchildview显示不正确
下面是代码的一些部分,你能帮我找出有什么事吗?非常非常感谢你!!
- 活动
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>
答
只是从其他人的覆盖布局,现在是正确的。 这里是我的新布局:
- work_item_h.xml
<TextView android:id="@+id/tv_worktype" android:layout_width="259dp" android:layout_height="wrap_content" android:layout_gravity="left|center" android:paddingBottom="10px" android:paddingLeft="40px" android:paddingTop="10px" android:textColor="#ff0000" android:textAppearance="?android:attr/textAppearanceLarge" android:textSize="20.0px" /> <ImageView android:id="@+id/iv_worktype_counter" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="right|center" android:src="@drawable/counter" /> </LinearLayout>
- work_item_c.xml
<ImageView android:id="@+id/iv_work_item_hl" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginBottom="2.0px" android:layout_marginLeft="10.0px" android:layout_marginTop="10.0px" android:paddingTop="6.0px" /> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginBottom="2.0px" android:baselineAligned="@+id/iv_work_item_hl" android:orientation="vertical" android:paddingTop="2.0px" > <TextView android:id="@+id/tv_work_item_title" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="left|center" android:layout_marginLeft="10.0px" android:textColor="#ff000000" android:textSize="20.0px" /> <TextView android:id="@+id/tv_work_item_applier" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="left|center" android:layout_marginLeft="10.0px" android:textColor="#ff838383" android:textSize="13.0px" /> </LinearLayout>
但我仍然不知道什么是我以前的布局问题,它在我的日食布局设计师正确显示。无论如何,我很高兴看到它最终奏效。谢谢任何人......,似乎没有人关心我的问题,然后感谢我自己,感谢上帝:)
Regards, Jeff