微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

SwipeRefreshLayout wrap_content无法在Dialogfragment中使用ListView

如何解决SwipeRefreshLayout wrap_content无法在Dialogfragment中使用ListView

我试图将包含子项SwipeRefreshLayout的{​​{1}}设置为ListView中的 wrap_content

实际上是使对话框包裹其子级的高度,因为即使 ListView 仅具有一项。

布局:

DialogFragment

结果:

enter image description here

我需要根据 ListView 项来设置 DialogFragment 窗口缩放高度,而不是始终填充窗口高度。

通过制作<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content"> <RelativeLayout android:id="@+id/footer" android:layout_alignParentBottom="true" android:layout_width="match_parent" android:layout_height="wrap_content"> <LinearLayout android:layout_width="match_parent" android:layout_height="1dp" android:background="@color/devider_line"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="@string/myButton" /> </RelativeLayout> <androidx.swiperefreshlayout.widget.SwipeRefreshLayout android:layout_above="@id/footer" android:layout_width="match_parent" android:layout_height="wrap_content"> <ListView android:id="@+id/listview" android:layout_width="match_parent" android:layout_height="wrap_content" /> </androidx.swiperefreshlayout.widget.SwipeRefreshLayout> </RelativeLayout> SwipeRefreshLayout来包装内容(项目)。

我尝试了很多方法来做,但是没有用。

我该如何实现?

更新:

ListView本身正在填充整个窗口高度,而忽略了SwipeRefreshLayout

我用wrap_content取代了ListView,而RecyclerView包裹了内容高度,这很好。但RecyclerView仍然是填充高度,迫使 DialogFragment 覆盖所有 Window

我将对话框的高度设置为SwipeRefreshLayout

wrap_content

这是 list_item.xml:

@Override
public void onResume() {
    getDialog().getwindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT,WindowManager.LayoutParams.WRAP_CONTENT);

    super.onResume();
}

更新2:

使用<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content"> <LinearLayout android:id="@+id/container" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:padding="18dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <LinearLayout android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="0.4" android:gravity="start" android:orientation="horizontal" android:weightSum="1"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="start" android:text="@string/default_text" /> </LinearLayout> <LinearLayout android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="0.6" android:gravity="end" android:orientation="horizontal" android:weightSum="1"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/default_text" android:textAlignment="center" /> </LinearLayout> </LinearLayout> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="1dp" android:layout_below="@+id/container" android:background="@color/devider_color" /> </RelativeLayout> RecyclerView包装,以便 wrap_content 可以使用它。

LinearLayout

但是<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content"> <androidx.swiperefreshlayout.widget.SwipeRefreshLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#00ffff"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical"> <androidx.recyclerview.widget.RecyclerView android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#ff00ff" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/myButton" /> </LinearLayout> </androidx.swiperefreshlayout.widget.SwipeRefreshLayout> </LinearLayout> 仍在填充高度。

enter image description here

解决方法

ListView的行为类似,请尝试使用此子类覆盖此行为

public class WrapingHeightListView extends ListView {

    public WrapingHeightListView(Context context) {
        super(context);
    }

    public WrapingHeightListView(Context context,AttributeSet attrs) {
        super(context,attrs);
    }

    public WrapingHeightListView(Context context,AttributeSet attrs,int defStyle) {
        super(context,attrs,defStyle);
    }

    @Override
    public void onMeasure(int widthMeasureSpec,int heightMeasureSpec) {
        int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec,expandSpec);
    }

}

在XML中使用<com.yourpackage.WrapingHeightListView...代替当前的<ListView ...标记

或者您可以切换到RecyclerView,它可以正确处理wrap_content的高度(但这还需要一些小的适配器重构)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。