I have a ListView with its Custom Adapter.
in adapter's getview i want to set margin left of 15 dip to some rows. based on certain conditions.
-----------------------------
| Row 1 (margin 15dip) |
-----------------------------
-----------------------------
| Row 2 (margin 15dip) |
-----------------------------
-----------------------------
| Row 2.1 (margin 30dip) |
-----------------------------
-----------------------------
| Row 2.1.1 (margin 45dip) |
-----------------------------
-----------------------------
| Row 3 (margin 15dip) |
-----------------------------
But when i set margin using the following code it gives ClassCastException
01-17 18:19:35.467: E/AndroidRuntime(14165): java.lang.ClassCastException: android.widget.FrameLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams
Row layout
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/parentframeLayout" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="10dip" android:layout_marginRight="10dip" > <LinearLayout android:layout_width="match_parent" android:layout_height="40dip" android:background="@color/alphaBlueMariner" android:gravity="center_vertical" android:orientation="horizontal" android:paddingLeft="10dip" > <ImageView android:id="@+id/offlineImageView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center_vertical" android:src="@drawable/ic_object_offline" /> <ImageView android:id="@+id/parentImageView" android:layout_width="@dimen/small_indicator_icon_square" android:layout_height="@dimen/small_indicator_icon_square" android:layout_marginLeft="5dip" android:src="@drawable/ic_arrow_right" /> <TextView android:id="@+id/tvParentName" style="@style/TextViewMedStyleShadowBlack" android:layout_width="fill_parent" android:layout_height="wrap_content" android:paddingLeft="20dip" /> </LinearLayout> </FrameLayout>
sample in my getView method
Log.i("LayoutParams","1");
// setting left margin
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.MATCH_PARENT,
FrameLayout.LayoutParams.WRAP_CONTENT
);
Resources r = context.getResources();
int px = (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP,
15,
r.getDisplayMetrics()
);
if(requirementModel.getParentId() == 0)
{
params.setMargins(px, 0, 0, 0);
parentFrameLayout.setLayoutParams(params);
}
else
{
params.setMargins(px+px, 0, 0, 0);
parentFrameLayout.setLayoutParams(params);
}
parentFrameLayout.requestLayout();
}
return row;
this code runs but crashes when
return row;
Please help. thanks :)
ListView expects AbsListView.LayoutParams that does not support margins. Try to set margins on the LinearLayout object instead of the FrameLayout. Or use paddings on the FrameLayout.