簡體   English   中英

ListView行在CustomAdapter中以編程方式設置左邊距

[英]ListView row set Left Margin programmatically in CustomAdapter

我有一個帶有自定義適配器的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)       |
  -----------------------------

但是當我使用以下代碼設置邊距時,它會產生ClassCastException

01-17 18:19:35.467:E / AndroidRuntime(14165):java.lang.ClassCastException:android.widget.FrameLayout $ LayoutParams無法強制轉換為android.widget.AbsListView $ LayoutParams

行布局

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

此代碼運行但崩潰時

返回行;

請幫忙。 謝謝 :)

ListView期望不支持邊距的AbsListView.LayoutParams。 嘗試在LinearLayout對象而不是FrameLayout上設置邊距。 或者在FrameLayout上使用填充。

我懷疑是因為LayoutParams不是LinearLayout類型。 你能試試嗎?

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,      
                LinearLayout.LayoutParams.WRAP_CONTENT
                );

如果要在完整視圖中設置邊距,可以將列表視圖參數轉換為並完成工作,但可以按照其他人的建議設置單個項目邊距

ViewGroup.MarginLayoutParams marginLayoutParams = (ViewGroup.MarginLayoutParams) mListView
        .getLayoutParams();

marginLayoutParams.setMargins(leftMargin, topMargin, righttMargin, bottomMargin);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM