使用自定义适配器无法设置ListView中项目的边距

11

我试图为ListView中的项目设置左边距变量。问题出现在我的自定义Adapter的getView方法中:

    public View getView(int position, View convertView, ViewGroup parent) {
        if(convertView == null){
            Comment c = comments.get(position);
            convertView = inflater.inflate(R.layout.list_item, null);               
            (TextView)convertView.findViewById(R.id.story)).setText(c.message);


        }
            RelativeLayout.LayoutParams lp = new  RelativeLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT);
            lp.setMargins(0, c.getDepth() * 5, 0, 0);               
            convertView.setLayoutParams(lp);

        return convertView;
    }

我们膨胀了一个包含一些TextView的RelativeLayout布局,然后尝试使用RelativeLayout.LayoutParams设置外边距。但是出现了ClassCastException异常,普通的LayoutParams类型没有设置外边距的方法。

如何最好地设置外边距?

这里是错误信息:

java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams
at android.widget.ListView.setupChild(ListView.java:1779)
at android.widget.ListView.makeAndAddView(ListView.java:1748)
at android.widget.ListView.fillDown(ListView.java:670)
at android.widget.ListView.fillFromTop(ListView.java:727)
at android.widget.ListView.layoutChildren(ListView.java:1598)
at android.widget.AbsListView.onLayout(AbsListView.java:1273)
at android.view.View.layout(View.java:7192)
at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
at android.view.View.layout(View.java:7192)
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:7192)
at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
at android.view.View.layout(View.java:7192)
at android.view.ViewRoot.performTraversals(ViewRoot.java:1145)
at android.view.ViewRoot.handleMessage(ViewRoot.java:1865)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3835)
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:847)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
at dalvik.system.NativeStart.main(Native Method)
2个回答

7
我发现解决这个问题的最简单方法是在xml中使用FrameLayout包装我的项目。然后我可以轻松地设置所需的左内边距,从而获得我所希望的完美效果。

6
我不确定你的ClassCast异常出现在哪里,是在convertView.setLayoutParams(lp);吗?如果是的话,请提供你的list_item xml文件。由于你的布局是一个RelativeLayout,所以你必须提供一个RelativeLayout.LayoutParams,就像你做的那样。
编辑:由于是在ListView中,必须提供与父元素相同的LayoutParams,即AbsListView.LayoutParams。然而,这种布局没有提供margin设置器。在其他的stackoverflow线程中,我看到你必须这样膨胀布局:inflate(xml_id, listView, false);来明确表示与ListView和内容没有关联。然而,我也无法使它工作。
另外,我看到你在convertView创建时设置了边距。如果你这样做,那么只有最初创建的项目才会有自己的自定义边距。其他的将有回收视图的边距。我不认为这是你想要的。

实际上,这似乎是在我返回converView之后发生的,尽管您正确地指出我需要更早地设置边距。我已经将错误添加到我的帖子中。 - Patrick Dattilio
1
我制作了一个简单的应用程序来测试它,但没有成功使其工作。我建议您改用View padding,它具有相同的行为。convertView.setPadding(0, c.getDepth() * 5, 0, 0); 很抱歉无法提供更多帮助 :/ - Astrorvald
很不幸,我实际上想要整个视图被移动,而填充只会在视图内部创建空间。感谢您的尝试! - Patrick Dattilio

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接