任何人都知道如何正确地将 HeaderView 添加到 android 中的 ListView 中?这就是我所拥有的:
LayoutInflater inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View inflatedView = inflater.inflate(R.layout.adapter_post_note, commentsListView, false);
之后,我通过设置图像资源和/或文本来设置一个 ImageView 和几个 TextView,它们已经是 adapter_post_note 布局的一部分。然后这里是我添加 HeaderView
commentsListView.addHeaderView(inflatedView);
commentsAdapter = new CommentsAdapter(this, post.getComments(), post);
commentsListView.setAdapter(commentsAdapter);
适配器是扩展 BaseAdapter 的自定义适配器,我传入上下文、填充 ListView 的 List 以及出于某种原因的“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);
但它仍然不起作用