为ListView添加header

具体的ListView的addHeaderView方法网络上很多,但是我在使用时出现了错误

java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams
            at android.widget.ListView.clearRecycledState(ListView.java:568)
            at android.widget.ListView.resetList(ListView.java:554)
            at android.widget.ListView.layoutChildren(ListView.java:1587)
            at android.widget.AbsListView.onLayout(AbsListView.java:2152)
            at android.view.View.layout(View.java:14971)
            at android.view.ViewGroup.layout(ViewGroup.java:4658)
            at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
            at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
            at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
            at android.view.View.layout(View.java:14971)
            at android.view.ViewGroup.layout(ViewGroup.java:4658)
            at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
            at android.view.View.layout(View.java:14971)
            at android.view.ViewGroup.layout(ViewGroup.java:4658)
            at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
            at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
            at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
            at android.view.View.layout(View.java:14971)
            at android.view.ViewGroup.layout(ViewGroup.java:4658)
            at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
            at android.view.View.layout(View.java:14971)
            at android.view.ViewGroup.layout(ViewGroup.java:4658)
            at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2162)
            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1886)
            at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1017)
            at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5967)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
            at android.view.Choreographer.doCallbacks(Choreographer.java:574)
            at android.view.Choreographer.doFrame(Choreographer.java:544)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5032)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)



经过查错,发现是这样的

ListView中添加的是View而不是控件,我的错误是直接把控件添加到了ListView中。

为ListView添加header_第1张图片

这里lvSmallGame是ListView控件,fv是自定义的控件FlashView,作为在ListView第一行显示的控件。


参考资料:http://stackoverflow.com/questions/23287138/android-classcastexception-adding-headerview-to-listview

http://blog.csdn.net/hlglinglong/article/details/26491393

你可能感兴趣的:(为ListView添加header)