gpt4 book ai didi

返回 LinearLayout 的 Android 适配器

转载 作者:太空宇宙 更新时间:2023-11-03 11:45:37 25 4
gpt4 key购买 nike

我有一个用于 ListView 的自定义适配器,我需要从 getView() 返回 LinearLayout。我这样做的原因是我有自定义组件,它扩展了 LinearLayout 并且需要从代码创建。问题是,如果我从 getView() 返回任何 LinearLayout,应用程序就会崩溃。我怎样才能让它工作?我无法从 xml 扩展 View ,一切都必须通过代码完成。

这是 getView():

@Override
public View getView(int position, View convertView, ViewGroup parent)
{
LinearLayout lay = new LinearLayout(MyApplication.getContext()); // gets the context from extended Application class
lay.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
return lay;
}

记录发生的事情:

08-20 08:40:07.886: DEBUG/AndroidRuntime(13039): Shutting down VM
08-20 08:40:07.896: WARN/dalvikvm(13039): threadid=1: thread exiting with uncaught exception (group=0x4001d5a0)
08-20 08:40:07.926: ERROR/AndroidRuntime(13039): FATAL EXCEPTION: main
java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams
at android.widget.ListView.setupChild(ListView.java:1870)
at android.widget.ListView.makeAndAddView(ListView.java:1839)
at android.widget.ListView.fillDown(ListView.java:670)
at android.widget.ListView.fillFromTop(ListView.java:727)
at android.widget.ListView.layoutChildren(ListView.java:1689)
at android.widget.AbsListView.onLayout(AbsListView.java:1260)
at android.view.View.layout(View.java:7278)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1130)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1047)
at android.view.View.layout(View.java:7278)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:912)
at android.view.View.layout(View.java:7278)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254)
at android.widget.LinearLayout.layoutHorizontal(LinearLayout.java:1243)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1049)
at android.view.View.layout(View.java:7278)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1130)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1047)
at android.view.View.layout(View.java:7278)
at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
at android.view.View.layout(View.java:7278)
at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
at android.view.View.layout(View.java:7278)
at android.view.ViewRoot.performTraversals(ViewRoot.java:1172)
at android.view.ViewRoot.handleMessage(ViewRoot.java:1921)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:143)
at android.app.ActivityThread.main(ActivityThread.java:4196)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)

谢谢

//edit:如果有人能给我提供一个工作示例,它通过代码创建 LinearLayout 并从 getView() 返回它,它很可能会解决这个问题

最佳答案

设置 AbsListView.LayoutParams 而不是 LinearLayout.LayoutParams
LayoutParams 将为 ViewGroup 中的子项设置。由于您的布局是 AbsListView 的子级,因此您应该设置其布局参数。

关于返回 LinearLayout 的 Android 适配器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7130133/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com