在ExpandableListView的子视图布局中添加textview

时间:2015-04-10 07:37:57

标签: android android-layout expandablelistview baseadapter

我有expandableListview工作正常,但现在在子视图中我必须使它更大并添加更多信息,我需要为每个子项垂直添加更多textview。例如,子视图的布局应该是这样的

Type 1       Value1
Type 2       Value2
Type 3       Value3 

Category     Average

我已经有了这个类别和平均值,但现在需要子类型和各自的值。

为了达到这个目的,我试图给两个textview的新布局充气,然后添加到子视图。但是我不知道如何使它垂直的信息。 我正在分享一些试图这样做的代码。

  @Override
public View getChildView(int groupPosition, final int childPosition,
                         boolean isLastChild, View convertView, ViewGroup parent)
{
    holder = new ViewHolder();
    if (convertView == null)
    {
        holder = new ViewHolder();
        convertView = layoutInflater.inflate(R.layout.list_item, null);
        holder.sub_rl = (RelativeLayout) convertView.findViewById(R.id.sub_row_layout);
        holder.cat = (TextView) convertView.findViewById(R.id.sub_row_cat);
        holder.avg = (TextView) convertView.findViewById(R.id.sub_row_avg);
        convertView.setTag(R.id.sub_row_layout, holder.sub_rl);
        convertView.setTag(R.id.sub_row_cat, holder.cat);
        convertView.setTag(R.id.sub_row_avg, holder.avg);
        convertView.setTag(holder);
    }

    else
       if (getChild(groupPosition, childPosition).type.equals("Emp")) {

                    for(int i = 0; i < 5; i ++){
                        Log.d(TAG, "coming ");
                        LayoutInflater inflater2 = null;
                        inflater2 = (LayoutInflater) mContext.getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                        View mLinearView2 = inflater2.inflate(R.layout.type_layout, null);
                        LinearLayout wifi_layout = (LinearLayout) mLinearView2.findViewById(R.id.type_layout);
                        TextView mSubItemName = (TextView) mLinearView2.findViewById(R.id.row_type);
                        TextView sub_value = (TextView) mLinearView2.findViewById(R.id.row_value);
                        mSubItemName.setText("Sub Text "+ i);
                        holder.sub_rl.addView(wifi_layout);

                    }

                    if (getChild(groupPosition, childPosition).cat.isEmpty()) {
                        if (getChild(groupPosition, childPosition).cat != null && holder.cat != null)
                            holder.cat.setText("(Hidden Category)");
                    } else {
                        if (getChild(groupPosition, childPosition).cat != null && holder.cat != null)
                            holder.cat.setText(getChild(groupPosition,  childPosition).cat);
                    }
                    if (getChild(groupPosition, childPosition).avg != null && holder.avg != null)
                        holder.avg.setText(getChild(groupPosition, childPosition).avg);
 return convertView;
}

list_item.xml

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout   xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="55dip"
android:id="@+id/es_child_main"
android:layout_marginLeft="10dp"
android:orientation="vertical" >
<RelativeLayout
    android:id="@+id/sub_row_layout"
    android:layout_width="wrap_content"
    android:layout_marginLeft="15dp"
    android:layout_marginTop="5dp"
    android:layout_marginRight="5dp"
    android:layout_marginBottom="5dp"
    android:layout_height="wrap_content">
<TextView
    android:id="@+id/sub_row_cat"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="20dp"
    android:layout_marginTop="8dp"
    android:textColor="#ffffff"
    android:layout_marginLeft="10dp"
    />
<TextView
    android:id="@+id/sub_row_avg"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/sub_row_cat"
    android:textSize="15dp"
    android:textColor="#ffffff"
    android:layout_marginLeft="10dp"
    android:layout_marginTop="5dp"/>
   </RelativeLayout>
   </RelativeLayout>

任何建议都会很好......或者更好的方法谢谢

1 个答案:

答案 0 :(得分:0)

我建议使用线性布局或更好的表布局来满足您对子布局的需求,这对我有用:

    <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="5dp" >

    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Type 1" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:text="Value 1" />
    </TableRow>

    <TableRow
        android:id="@+id/tableRow2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Type 2" />

        <TextView
            android:id="@+id/textView4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:text="Value 2" />
    </TableRow>

    <TableRow
        android:id="@+id/tableRow3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/textView5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Type 3" />

        <TextView
            android:id="@+id/textView6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:text="Value 3" />
    </TableRow>

</TableLayout>