Sha*_*kar 3 android listview android-adapter layoutparams
我有一个带有自定义适配器的ListView.
在适配器的getview中,我想将15 dip的左边距设置为某些行.基于某些条件.
-----------------------------
| Row 1 (margin 15dip) |
-----------------------------
-----------------------------
| Row 2 (margin 15dip) |
-----------------------------
-----------------------------
| Row 2.1 (margin 30dip) |
-----------------------------
-----------------------------
| Row 2.1.1 (margin 45dip) |
-----------------------------
-----------------------------
| Row 3 (margin 15dip) |
-----------------------------
Run Code Online (Sandbox Code Playgroud)
但是,当我使用以下代码设置保证金时,它给出了 ClassCastException
01-17 18:19:35.467:E/AndroidRuntime(14165):java.lang.ClassCastException:android.widget.FrameLayout $ LayoutParams无法强制转换为android.widget.AbsListView $ LayoutParams
行布局
Run Code Online (Sandbox Code Playgroud)<?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>
我的getView方法中的示例
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;
Run Code Online (Sandbox Code Playgroud)
此代码运行但崩溃时
返回行;
请帮忙.谢谢 :)
ListView期望不支持边距的AbsListView.LayoutParams.尝试在LinearLayout对象而不是FrameLayout上设置边距.或者在FrameLayout上使用填充.
| 归档时间: |
|
| 查看次数: |
8631 次 |
| 最近记录: |