- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我有一个相当复杂的布局(包含RelativeLayouts、TextViews 和ImageViews),我想将它放在 ListView 上方。此 View 应与 ListView 一起滚动。
我尝试使用以下代码将布局作为标题添加到 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)
行,我不会收到错误消息。我也可以在没有 ListView 的情况下毫无问题地显示标题布局。
我假设这与标题布局的内容有关,但我不确定究竟是什么原因造成的。
这里是头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>
更新:我尝试将 header .xml 文件缩减为一个 TextView,但问题仍然存在。所以我不认为问题是由 xml 中的某些内容引起的。
最佳答案
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);
关于android - 调用 ListView.addHeaderView() 时出现 ClassCastException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8275669/
我正在使用 mList.addHeaderView(mHeader, null, false); 在 Android 中,向我的列表添加一个(不可选择的)标题。呈现列表时,标题不可选择,但标题底部和第
我有一个 RelativeLayouts,我想将其作为标题添加到 ExpandableList。基本上,布局与我用于列表中 subview 的布局相同。 布局如下:
我正在使用以下布局 (header.xml) 在 ListView 中添加标题, 另一方面,我正在使用, View header = (View) getLayoutInflater().in
我正在寻找与 addHeaderView 等效的回收器 View 。基本上我想将带有 2 个按钮的图像作为标题添加到 ListView 中。有没有其他方法可以将标题 View 添加到回收站 View
下面是一个带有 ListView 的代码 fragment 。我添加了一个emptyView 和一个headerView。添加 headerView 会导致 onItemClick 中的位置增加一。
我正在膨胀 View 并将其添加到 ListView 以将其用作 ListView 中顶部元素的填充。问题是 View 不会在 ListView 中呈现,除非我将 View 更改为例如 TextVie
当我尝试在我的 ListFragment 的列表上添加 header 时遇到问题。我听说我必须在 setListAdapter 之前添加 header View (我遵循了这个主题 Best plac
我正在寻找与 addHeaderView 等效的回收站 View 。基本上我希望将带有 2 个按钮的图像作为标题添加到 ListView 。是否有不同的方法可以将标题 View 添加到回收站 View
Android 的 addHeaderView() 能否用于在单个 ListView 中添加多个标题?有人可以举例说明如何执行此操作吗? 我能够通过操纵 IconicAdapter 类来完成我想要的事
我有一个相当复杂的布局(包含RelativeLayouts、TextViews 和ImageViews),我想将它放在 ListView 上方。此 View 应与 ListView 一起滚动。 我尝试
我正在使用 Commonsware 的 EndlessAdapter。当我添加了一个 header View 时,我得到了我的 listView,我得到了这个错误和 LogCat: 04-22 17:
我想知道为什么下面的代码不能正常工作。 @Override public void onUpdateView(View view, int position, NewsItemNormal dat
我的目标很简单。我想将“红色矩形”作为 headerview 添加到 ListView。所以我创建了一个简单的 Activity public class MainActivity extends A
是否有一个 XML 标记,我可以在布局文件中使用它等同于 ListView.addHeaderView() ? 最佳答案 我按照您的要求编写了一个简单的 ListView。 在 value 文件夹的
我尝试在我的新应用程序中使用 StaggeredGridView。一切正常。现在我想添加一个 HeaderView 和一个 FooterView,但这给了我以下错误: java.lang.Illega
尝试将 addHeaderView() 和 addFooterView() 用于 ListView。如果我尝试使用我在 XML 中为页眉或页脚预定义的 View ,我会得到一个空指针异常。但是,如果我
我是一名优秀的程序员,十分优秀!