1

我正在尝试将SwipeRefreshLayout包含子ListView的内容设置为.DialogFragment

实际上使Dialog包装它的孩子的高度,因为即使ListView只有一项,它也总是填充窗口高度

布局 :

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

结果 :

在此处输入图像描述

我需要根据ListView项目使DialogFragment窗口缩放高度,而不总是填充窗口高度。

通过制作 theSwipeRefreshLayout或 theListView来包装内容(项目)。

我已经尝试了很多方法来做到这一点,但没有任何效果。

我怎样才能做到这一点?

更新 :

SwipeRefreshLayout本身正在填充整个窗口高度并忽略wrap_content.

我替换ListView为包装内容高度RecyclerViewRecyclerView这很好。但仍然SwipeRefreshLayout填充高度,迫使DialogFragment覆盖所有Window

我将对话框高度设置为wrap_content

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

    super.onResume();
}

这是list_item.xml :

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

更新 2:

使用RecyclerView包装它,LinearLayout这样wrap_content就可以了。

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

但仍然SwipeRefreshLayout是在填补高度。

在此处输入图像描述

4

1 回答 1

0

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);
    }

}

<com.yourpackage.WrapingHeightListView...在您的 XML 中使用而不是当前<ListView ...标记

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

于 2020-10-12T05:46:59.743 回答