0
我知道這個問題在許多帖子中仍然有答案。理論上我知道答案,但在這種情況下,我無法得到它的工作。在ListView中以編程方式插入自定義LinearLayout
我有一個自定義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;
}
得到它的工作,我必須把一些layoutparameters在該行的構造函數和移動從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以編程實例化改變,該列表視圖在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
什麼錯呢? 謝謝!