我有一个相当复杂的布局(包含RelativeLayouts,TextViews和ImageViews),我想放在listview上面。此视图应滚动列表视图。
我尝试使用以下代码将布局作为标题添加到列表视图中:
View v = inflater.inflate(R.layout.list_view, container, false);
View header = inflater.inflate(R.layout.header_layout, container, false);
// populate views in the header
mList = (ListView)v.findViewById(R.id.list);
mList.addHeaderView(header);
mAdapter = new ReviewsAdapter(getActivity());
mList.setAdapter(mAdapter); <-- error occurs here
ReviewsAdapter
是我编写的自定义适配器,扩展了BaseAdapter
。
执行代码后,我收到此错误:
11-25 17:19:14.802: E/AndroidRuntime(1215): java.lang.ClassCastException: android.widget.FrameLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams
11-25 17:19:14.802: E/AndroidRuntime(1215): at android.widget.ListView.clearRecycledState(ListView.java:513)
11-25 17:19:14.802: E/AndroidRuntime(1215): at android.widget.ListView.resetList(ListView.java:499)
11-25 17:19:14.802: E/AndroidRuntime(1215): at android.widget.ListView.setAdapter(ListView.java:442)
11-25 17:19:14.802: E/AndroidRuntime(1215): at com.coppi.storefront.product.ProductReviewsFragment.onCreateView(ProductReviewsFragment.java:104)
如果我注释掉mList.addHeaderView(header)
行,我就不会收到错误消息。我也可以在没有列表视图的情况下显示标题布局而没有任何问题。
我认为这与标题布局的内容有关,但我不确定究竟会导致它的原因。
这是标题xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RelativeLayout
android:id="@+id/header_section"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/margin_sides"
android:background="@color/pdp_availability_section_background" >
<TextView
android:id="@+id/header_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/margin_sides"
android:paddingBottom="@dimen/margin_sides"
android:text="@string/ratings_reviews"
android:textColor="#000"
android:textSize="18dp" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/body_section"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@id/header_section" >
<TextView
android:id="@+id/product_title"
style="@style/ProductTitleFont"
android:layout_marginBottom="@dimen/product_title_bottom_margin"
android:layout_marginLeft="@dimen/margin_sides"
android:layout_marginRight="@dimen/margin_sides" />
<RelativeLayout
android:id="@+id/attributes_section"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/product_title"
android:layout_centerHorizontal="true"
android:layout_margin="@dimen/margin_sides" >
<LinearLayout
android:id="@+id/overall_section"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="bottom" >
<TextView
android:id="@+id/overall_label"
style="@style/ProductTitleFont"
android:layout_width="wrap_content"
android:text="@string/overall_rating" />
<ImageView
android:id="@+id/overall_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:src="@drawable/icon_rating_empty" />
<ImageView
android:id="@+id/overall_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/icon_rating_empty" />
<ImageView
android:id="@+id/overall_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/icon_rating_empty" />
<ImageView
android:id="@+id/overall_4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/icon_rating_empty" />
<ImageView
android:id="@+id/overall_5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/icon_rating_empty" />
<TextView
android:id="@+id/overall_score"
style="@style/ProductTitleFont"
android:layout_width="wrap_content"
android:layout_marginLeft="10dp"
android:text="4.6" />
</LinearLayout>
<Button
android:id="@+id/rate_review_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/overall_section"
android:layout_marginBottom="@dimen/margin_sides"
android:layout_marginTop="@dimen/margin_sides"
android:text="@string/rate_review_button_text" />
</RelativeLayout>
<View
android:id="@+id/attributes_divider"
android:layout_width="fill_parent"
android:layout_height="1dp"
android:layout_below="@id/attributes_section"
android:layout_marginBottom="@dimen/margin_sides"
android:layout_marginTop="@dimen/margin_sides"
android:background="@color/pdp_section_divider" />
<TextView
android:id="@+id/review_count"
style="@style/ProductTitleFont"
android:layout_width="wrap_content"
android:layout_below="@id/attributes_divider"
android:layout_marginLeft="@dimen/margin_sides"
android:text="0 " />
<TextView
style="@style/ProductTitleFont"
android:layout_width="wrap_content"
android:layout_alignBaseline="@id/review_count"
android:layout_marginRight="@dimen/margin_sides"
android:layout_toRightOf="@id/review_count"
android:text="@string/customer_reviews" />
<View
android:id="@+id/review_count_divider"
android:layout_width="fill_parent"
android:layout_height="1dp"
android:layout_below="@id/review_count"
android:layout_marginBottom="@dimen/margin_sides"
android:layout_marginTop="@dimen/margin_sides"
android:background="@color/pdp_section_divider" />
</RelativeLayout>
</RelativeLayout>
更新:我尝试将标题.xml文件缩减为单个TextView,问题仍然存在。所以我不相信问题是由xml中的某些东西引起的。
答案 0 :(得分:75)
FrameLayout
和AbsListView
将其子级布局参数转换为FrameLayout.LayoutParams
和AbsListView.LayoutParams
。这是铸造失败的地方。
View header = View.inflate(this, R.layout.header_layout, null);
应该修复它。
编辑: 正如评论中所提到的,更改inflate调用的ViewGroup参数也可以使其工作:
header = inflater.inflate(R.layout.header_layout, null, false);
答案 1 :(得分:16)
虽然Souvlaki的方法将解决问题并允许您继续工作。最好将视图夸大其对容器的引用。这样Android就可以正确地为正确的上下文充气。
你应该做的是找到ListView
,然后将其传递给充气。
ListView listView = (ListView) layout.findViewById(R.id.listview);
View header = inflater.inflate(this, R.layout.header_layout, listView, false);
答案 2 :(得分:2)
我看到同样的情况。 在我的情况下,调用ListView.addHeaderView()的时间是一个麻烦。
之前:(错误)
ListView.addHeaderView()在Fragment.onCreateView()方法中调用。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View layout = inflater.inflate(R.layout.fragment_main, container, false);
mListView = (ListView) layout.findViewById(R.id.listview);
mListView.addHeaderView(inflater.inflate(R.layout.list_header_book, listview, false));
return layout;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mAdapter = new BookAdapter(getActivity(), mBookList);
mListView.setOnItemClickListener(mBookItemClickListener);
mListView.setAdapter(mAdapter);
}
的后:(OK)强>
在Fragment.onActivityCreated()方法中调用ListView.addHeaderView()。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_main, container, false);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mAdapter = new BookAdapter(getActivity(), mBookList);
mListView = (ListView) getView().findViewById(R.id.listview);
mListView.setOnItemClickListener(mBookItemClickListener);
mListView.addHeaderView(LayoutInflater.from(getActivity()).inflate(R.layout.list_header_book, null));
mListView.setAdapter(mAdapter);
}
答案 3 :(得分:1)
来自drspaceboo's post above,这对我有用:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
mListLayoutView = inflater.inflate(getLayoutId(), container, false);
int headerLayoutId = R.layout.list_header_layout;
mListHeaderView = inflater.inflate(headerLayoutId, mResourcesListView, false);
// ...
mResourcesListView.addHeaderView(mListHeaderView, null, false);
其中getLayoutId()返回包含ListView xml
的主布局