以编程方式在ListView中插入自定义LinearLayout

时间:2013-02-27 00:41:02

标签: android android-layout

我知道很多帖子仍然会回答这个问题。理论上我知道答案,但在这种情况下,我无法让它发挥作用。

我有自定义BaseAdapter

@Override
public Row getView(int position, View convertView, ViewGroup parent)
{
    ...

    Row view = (Row) convertView;
    LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

    if (view == null)
    {
        view = (JVxDataRow) mInflater.inflate(R.layout.row_base, null);;
        ...
    }
    ...
}
    return view;
}

这很好用,row_base.xml是正确的,我的列表视图获取所有条目。 row_base.xml仅包含:

<?xml version="1.0" encoding="utf-8"?>
<mh.android.ui.Row
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/datarow"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="20dp"
android:paddingTop="20dp"
android:paddingBottom="20dp"
android:paddingRight="20dp" />

因此我想删除该文件,而是以编程方式实例化。 我的代码 - 不起作用 - 如下:

@Override
public Row getView(int position, View convertView, ViewGroup parent)
{
    ...

    Row view = (Row) convertView;
    LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

    if (view == null)
    {
        view = new Row(context);
        ...
    }
    ...
}
    return view;
}

要使它工作,我必须在Row的构造函数中放置一些布局参数,并将inflate从onFinishInflate移动到构造函数:

public Row(Context context)
{
    super(context);
    AbsListView.LayoutParams params = new AbsListView.LayoutParams(AbsListView.LayoutParams.WRAP_CONTENT, AbsListView.LayoutParams.WRAP_CONTENT);

    this.setLayoutParams(params);
    this.setPadding(20, 20, 20, 20);
    this.setOrientation(LinearLayout.VERTICAL);

    inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    container = inflater.inflate(R.layout.datarow3, this, true);

    this.addView(container);
}

datarow3.xml看起来:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/table_row_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<mh.android.ui.DataItem
    android:id="@+id/dataitem_slot1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:singleLine="true" >
</mh.android.ui.DataItem>
<mh.android.ui.DataItem
    android:id="@+id/dataitem_slot2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:singleLine="true" >
</mh.android.ui.DataItem>
</LinearLayout>

问题出现在BaseAdapter中,当我从编程扩展xml到实例化时,listview位于LinearLayout内。

现在例外:

E/AndroidRuntime(13203): FATAL EXCEPTION: main
E/AndroidRuntime(13203): java.lang.ClassCastException:     android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams
E/AndroidRuntime(13203):    at android.widget.ListView.measureScrapChild(ListView.java:1170)
E/AndroidRuntime(13203):    at android.widget.ListView.measureHeightOfChildren(ListView.java:1252)
E/AndroidRuntime(13203):    at android.widget.ListView.onMeasure(ListView.java:1162)
E/AndroidRuntime(13203):    at android.view.View.measure(View.java:15288)
E/AndroidRuntime(13203):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4876)
E/AndroidRuntime(13203):    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1396)
E/AndroidRuntime(13203):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
E/AndroidRuntime(13203):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
E/AndroidRuntime(13203):    at android.view.View.measure(View.java:15288)
E/AndroidRuntime(13203):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4876)
E/AndroidRuntime(13203):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
E/AndroidRuntime(13203):    at android.view.View.measure(View.java:15288)
E/AndroidRuntime(13203):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:833)
E/AndroidRuntime(13203):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
E/AndroidRuntime(13203):    at android.view.View.measure(View.java:15288)
E/AndroidRuntime(13203):    at 
那边出了什么问题? 感谢的!

1 个答案:

答案 0 :(得分:1)

为什么所有评论都消失了? 我的问题的解决方案:

container = inflater.inflate(R.layout.datarow3, this, true);
this.addView(container);

更改为

inflater.inflate(R.layout.datarow3, this, true);

在充气后不需要添加布局。