任何人都知道如何在Android中正确添加HeaderView到ListView?这就是我所拥有的:
LayoutInflater inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View inflatedView = inflater.inflate(R.layout.adapter_post_note, commentsListView, false);
之后我通过设置图像资源和/或文本来设置ImageView和一些已经成为adapter_post_note布局一部分的TextView。然后在这里我添加了HeaderView
commentsListView.addHeaderView(inflatedView);
commentsAdapter = new CommentsAdapter(this, post.getComments(), post);
commentsListView.setAdapter(commentsAdapter);
适配器是一个自定义适配器,它扩展了BaseAdapter,我在上下文中传递了List,它填充了ListView,由于某种原因我发布了“post”,但这并不重要。
这是我得到的错误
07-16 12:39:58.858: E/AndroidRuntime(1562): FATAL EXCEPTION: main
07-16 12:39:58.858: E/AndroidRuntime(1562): java.lang.ClassCastException: android.widget.FrameLayout$LayoutParams
07-16 12:39:58.858: E/AndroidRuntime(1562): at android.widget.LinearLayout.measureVertical(LinearLayout.java:360)
07-16 12:39:58.858: E/AndroidRuntime(1562): at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
07-16 12:39:58.858: E/AndroidRuntime(1562): at android.view.View.measure(View.java:8372)
07-16 12:39:58.858: E/AndroidRuntime(1562): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
07-16 12:39:58.858: E/AndroidRuntime(1562): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
07-16 12:39:58.858: E/AndroidRuntime(1562): at android.view.View.measure(View.java:8372)
07-16 12:39:58.858: E/AndroidRuntime(1562): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
07-16 12:39:58.858: E/AndroidRuntime(1562): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
07-16 12:39:58.858: E/AndroidRuntime(1562): at android.view.View.measure(View.java:8372)
07-16 12:39:58.858: E/AndroidRuntime(1562): at android.view.ViewRoot.performTraversals(ViewRoot.java:844)
07-16 12:39:58.858: E/AndroidRuntime(1562): at android.view.ViewRoot.handleMessage(ViewRoot.java:1868)
07-16 12:39:58.858: E/AndroidRuntime(1562): at android.os.Handler.dispatchMessage(Handler.java:99)
07-16 12:39:58.858: E/AndroidRuntime(1562): at android.os.Looper.loop(Looper.java:130)
07-16 12:39:58.858: E/AndroidRuntime(1562): at android.app.ActivityThread.main(ActivityThread.java:3701)
07-16 12:39:58.858: E/AndroidRuntime(1562): at java.lang.reflect.Method.invokeNative(Native Method)
07-16 12:39:58.858: E/AndroidRuntime(1562): at java.lang.reflect.Method.invoke(Method.java:507)
07-16 12:39:58.858: E/AndroidRuntime(1562): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
07-16 12:39:58.858: E/AndroidRuntime(1562): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
07-16 12:39:58.858: E/AndroidRuntime(1562): at dalvik.system.NativeStart.main(Native Method)
我已经尝试将inflatedView的布局参数设置为ListView.LayoutParams,FrameLayout.LayoutParams,AbsListView.LayoutParams,没有任何效果。在我看来好像它正在添加inflatedView作为标题就好了,但后来当它试图使屏幕无效时,它试图通过并回收ListView子状态并且它失败,这只是一个猜测,但任何想法?
另外,我尝试过的另一件事是
View.inflate(this, R.layout.adapter_post_note, null);
但它仍然无效
答案 0 :(得分:1)
将父视图设置为null,您就可以开始使用了。无需引用视图的父级。只需将其充气并将其传递给方法。