我有类似的情况 - list_view.setAdapter(); 上有异常“java.lang.ClassCastException:android.widget.RelativeLayout$LayoutParams”;
我已经设法通过对页脚和列表项使用相同的布局来解决这个问题。下面的代码演示了如何在页脚和项目中使用布局“listview_row”。
这是“listview_row.xml”的内容:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/hs_line"
android:layout_alignParentLeft ="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<!-- TextView for list items -->
<TextView android:id="@+id/hs_line_textview"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_alignParentLeft="true"
/>
<!-- Button for footer -->
<Button android:id="@+id/hs_line_footer_action"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:visibility="gone"
android:layout_alignParentLeft="true"
/>
</RelativeLayout>
页脚以这种方式初始化:
View v = View.inflate(pager.Activity, R.layout.listview_row, null);
View footer = (View) v.findViewById(R.id.listview_line);
//footer doesn't use TextView, it uses Button only. So, we hide TextView.
footer.findViewById(R.id.hs_line_textview).setVisibility(View.GONE);
footer.findViewById(R.id.hs_line_footer_action).setVisibility(View.VISIBLE)
...
list_view.addFooterView(footer);
MyAdapter<Item> adapter = new MyAdapter(context, getListItems());
list_view.setAdapter(adapter);
适配器:
public class MyAdapter<T> extends ArrayAdapter<T> {
private final ArrayList<T> _List;
private final LayoutInflater _Inflater;
public MyAdapter(Context context, ArrayList<T> srcList) {
super(context, 0, srcList);
_Inflater = LayoutInflater.from(context);
_List = srcList;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = _Inflater.inflate(R.layout.listview_row, null);
holder = new ViewHolder();
//item doesn't use Button, it uses TextView only
//Button is hidden by default (see xml)
holder.TextView = (TextView) convertView.findViewById(R.id.hs_line_textview);
} else {
holder = (ViewHolder) convertView.getTag();
}
//item initialization
.....
}
这不是理想的解决方案 - 当然只有解决方法。