gpt4 book ai didi

android - 带有 SwipeRefresh 的 ListView 上的 MarginLayoutParams ClassCastException

转载 作者:行者123 更新时间:2023-11-30 00:39:04 24 4
gpt4 key购买 nike

我在 ListView 上实现下拉刷新手势,我有这个简单的布局 xml

<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 它工作正常。为什么会这样?

最佳答案

SwipeRefreshLayout 未使用为其子项扩展 MarginLayoutParams 的自定义 LayoutParams。它只是使用 ViewGroup.LayoutParams。这就是为什么如果您在 xml 中为 SwipeRefreshLayout 的 子项设置边距,它不会有任何结果。

这就是为什么您不能将 getLayoutParams() 的结果转换为 ViewGroup.MarginLayoutParams 的原因。

关于android - 带有 SwipeRefresh 的 ListView 上的 MarginLayoutParams ClassCastException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42854664/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com