如何动态地将行视图添加到线性布局,同时保持它们的唯一性

时间:2014-08-12 23:36:42

标签: android layout android-linearlayout unique dynamically-generated

我不知道这是否是最好的方法,但我有一个像这样的LinearLayout容器:

    <LinearLayout
        android:id="@+id/pnlPrefixItems"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="1dp"
        android:background="@color/panel_border">

        ... items here ...

    </LinearLayout>

这将包含子项,但我不知道运行时有多少。每个子项都有三个TextView:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:orientation="horizontal">

    <TextView
        android:id="@+id/txtArabic"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="center_vertical"
        android:padding="7dp"
        android:textSize="18sp"
        android:background="@color/list_item_arabic_background"
        android:textColor="@color/list_item_arabic_foreground"
        android:minWidth="25dp"
        android:text="@string/panel_arabic_sample"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:id="@+id/txtMeaning"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="left"
            android:background="@color/panel_content"
            android:textSize="20sp"
            android:paddingLeft="7dp"
            android:paddingRight="7dp"
            android:paddingTop="7dp"
            android:paddingBottom="1dp"
            android:text="@string/panel_meaning_sample"/>

        <TextView
            android:id="@+id/txtTechnical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="left"
            android:background="@color/panel_content"
            android:textColor="@color/match_pos_subtitle"
            android:textSize="12sp"
            android:paddingLeft="7dp"
            android:paddingRight="7dp"
            android:paddingTop="0dp"
            android:paddingBottom="7dp"
            android:text="@string/panel_technical_sample"/>
    </LinearLayout>
</LinearLayout>

在运行时,当我有数据时,我给子行布局充气并动态添加到父容器,一切似乎都很好,但是所有行最终都是相同的,设置为最后一行的值:

private void clearContainerPanels()
{
    __pnlPrefixItems.removeAllViews();
    ...
}

private void populateDetails(Match match)
{
    clearContainerPanels(); // clear all child rows first

    // add new child rows
    for (Pos _p : match.Prefix.Items)
    {
        LinearLayout _itemRow = (LinearLayout) __inflater.inflate(R.layout.panel_row, __pnlPrefixItems);
        TextView _txtArabic = (TextView) _itemRow.findViewById(R.id.txtArabic);
        TextView _txtMeaning = (TextView) _itemRow.findViewById(R.id.txtMeaning);
        TextView _txtTechnical = (TextView) _itemRow.findViewById(R.id.txtTechnical);
        _txtArabic.setText(_p.Vowelled);
        _txtMeaning.setText(_p.Meaning);
        _txtTechnical.setText(_p.Technical);
    }

}

如果有3行,则所有三行都具有第3行的值。如何最好地解决这个问题?我知道我可以使用列表,但是目前这似乎有点过分了。

最终结果:

enter image description here

1 个答案:

答案 0 :(得分:1)

此行有错误:

LinearLayout _itemRow = (LinearLayout) __inflater.inflate(R.layout.panel_row, __pnlPrefixItems);

应该改为:

LinearLayout _itemRow = (LinearLayout) __inflater.inflate(R.layout.panel_row, __pnlPrefixItems, false);
__pnlPrefixItems.addView(_itemRow);

说明:inflate()返回视图组,而不是膨胀视图,除非添加了布尔第三个arg(并且它必须为false)。因此,findViewById()将具有错误的范围。

来源:LayoutInflater javadoc