2017-03-17 65 views
1

我採取拉可刷新的ListView手勢ListView和我有這個簡單的佈局XMLMarginLayoutParams ClassCastException異常與SwipeRefresh

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <android.support.v4.widget.SwipeRefreshLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/swipeContainer_lgpin" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <ListView 
      android:id="@+id/listView" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:headerDividersEnabled="true" 
      android:footerDividersEnabled="true" 
      android:dividerHeight="5dp" 
      android:divider="@color/grey" 
      android:paddingLeft="5dp" 
      android:paddingRight="5dp" 
      android:orientation="vertical" 
      android:scrollbarStyle="outsideOverlay" /> 

    </android.support.v4.widget.SwipeRefreshLayout> 

    <ProgressBar 
     android:id="@+id/progressbar" 
     style="?android:attr/progressBarStyleLarge" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerInParent="true" 
     android:layout_gravity="center" 
     android:visibility="gone" /> 

</RelativeLayout> 

由於我使用SwipeRefreshLayout它給一個ClassCastException

java.lang.ClassCastException: android.view.ViewGroup$LayoutParams cannot be cast to android.view.ViewGroup$MarginLayoutParams 

在這個非相關的代碼,我用於其他目的

ViewGroup.MarginLayoutParams mlp = (ViewGroup.MarginLayoutParams)listview.getLayoutParams(); 
mlp.setMargins(0, 0, 0, 300); 

如果我刪除SwipeRefreshLayout,它工作正常。這是爲什麼發生?

回答

1

SwipeRefreshLayout沒有使用延伸MarginLayoutParams爲其子女的自定義LayoutParams。這只是使用ViewGroup.LayoutParams。這就是爲什麼它沒有任何結果,如果你在xml中設置SwipeRefreshLayout's孩子的保證金。

這就是爲什麼你不能將結果getLayoutParams()轉換爲ViewGroup.MarginLayoutParams

+0

還有什麼替代方案呢? (在列表視圖子上放置一個下降邊界) –

+0

根據您的確切需求,您可以:1.在SwipeRefreshLayout上設置'bottomPadding'或者2.在'ListView'和'clipToPadding'上設置'bottomPadding'設置爲false –

+0

我在列表視圖中放置了一個footerview,因爲我必須以編程方式刪除它。謝謝! –