Unable to set margin of item in ListView using custom Adapter
Asked Answered
C

2

11

I am attempting to set variable left margins for items in my ListView. The problem occurs in my custom Adapter in the getView method:

    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;
    }

We inflate the list_item layout which is a RelativeLayout with some textViews inside. I then attempt to set the margins using a RelativeLayout.LayoutParams. I get a class cast exception on this, but the normal LayoutParams type has no method for setting the margins.

What is the best way to go about setting the margin?

Here is the error

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)
Crosshatch answered 14/8, 2011 at 20:3 Comment(0)
C
7

I found the simplest way to solve this problem was to simply wrap my item in a FrameLayout in the xml. I could then easily set the left padding as I wished thus giving me the exact look I was hoping for.

Crosshatch answered 26/9, 2011 at 19:40 Comment(0)
E
6

I'm not sure where you have your ClassCast exception, is it on convertView.setLayoutParams(lp); ? If yes, then supply your list_item xml file please. As your layout is a RelativeLayout, then you have to supply a RelativeLayout.LayoutParams, as you did.

EDIT: Due to the ListView, the same LayoutParams as parent has to be supplied, a AbsListView.LayoutParams. However, this kind of layout doesn't provide the margin setter. On other stackoverflow thread, I saw that you have to inflate the layout like this: inflate(xml_id, listView, false); to explicitely said there is no connection with the ListView and the content. However, I couldn't make it working either.

Plus, I see you're setting the margin in the convertView creation. If you do like this, then only the items which are firstly created will have their own custom margin. Other will have the recycled view margin. I don't think it's what you want.

Eusebioeusebius answered 14/8, 2011 at 22:44 Comment(3)
It actually seems to occur after I return the converView, though you are correct that I need to set the margin earlier. I added the error to my post.Crosshatch
I made a simple app to test it, but didn't manage to make it working. I suggest you use View padding instead, which has I think the same behaviour. convertView.setPadding(0, c.getDepth() * 5, 0, 0); Sorry to don't bring more help :/Eusebioeusebius
Unfortunately I actually want the entire view to be shifted over, and padding only creates a space within the view. Thanks for trying!Crosshatch

© 2022 - 2024 — McMap. All rights reserved.