expandablelistview getchildview显示不正确

时间:2013-04-10 14:03:28

标签: expandablelistview fill-parent

我是android新手,我现在正尝试使用ExpandableListView首先列出组级别的工作类型,然后在子级别中列出工作项目。但是布局fill_parent在getChildView方法中不起作用很奇怪,它就像wrap_content一样,当我记录它们在布局中设置的时候,相关的textviews的高度也变为0,因此除非我手动设置文本大小,否则所有textview看起来都不见了在代码中。最奇怪的是只有子视图有问题,组级别工作正常,我不需要设置任何文本大小或高度。

下面是一些代码,请你帮我找出问题所在?非常感谢你!!

  1. 活动
  2. 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 
        }
        }
    
    1. 组级布局:work_items_h.xml //正常工作
    2. <?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>
      
      1. 子级别布局:work_items_c.xml // fill_parent不起作用,文本视图高度
      2. <?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>
        
             

1 个答案:

答案 0 :(得分:0)

只是覆盖其他人的布局,现在它是正确的。 这是我的新布局:

  1. work_item_h.xml
  2.   

          

        <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>
    
    1. work_item_c.xml
    2.   

      <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>
      

      但是我仍然不知道我以前的布局有什么问题,它在我的eclipse布局设计器中正确显示。无论如何,我很高兴看到它终于奏效了。谢谢任何人......,似乎没有人关心我的问题,然后感谢自己,感谢上帝:)

      此致 杰夫