【问题标题】:Adding textview in childview layout of ExpandableListView在 ExpandableListView 的子视图布局中添加 textview
【发布时间】:2015-04-10 07:37:57
【问题描述】:

我的 expandableListview 工作正常,但现在在 childview 中,我必须将其放大并添加更多信息,从某种意义上说,我需要为每个孩子垂直添加更多 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>

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

【问题讨论】:

  • 发布您的 list_item.xml 代码
  • 更新问题

标签: android android-layout expandablelistview baseadapter


【解决方案1】:

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

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

【讨论】:

  • 但我不明白你的意思,为什么我应该将表格布局用于我的主要布局,即 list_item?\
  • 要得到你在下面发布的结果'例如,子视图的布局应该是这样的'。如果我没记错的话,你想要单子视图中的那三行对吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-31
  • 1970-01-01
  • 1970-01-01
  • 2014-04-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多