0

我知道这个问题仍然在许多帖子中得到回答。理论上我知道答案,但在这种情况下我无法让它工作。

我有一个自定义 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>

当我从膨胀 xml 更改为以编程方式实例化时,问题出在 BaseAdapter 中,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 

那里出了什么问题?谢谢!

4

1 回答 1

1

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

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

变成

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

充气后不需要添加布局。

于 2013-02-28T12:05:25.933 回答