【问题标题】:SwipeRefreshLayout wrap_content not working with ListView in DialogfragmentSwipeRefreshLayout wrap_content 不适用于 Dialogfragment 中的 ListView
【发布时间】:2021-01-26 10:26:51
【问题描述】:

我正在尝试在DialogFragment 中将包含子ListViewSwipeRefreshLayout 设置为wrap_content

真正让 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 窗口缩放高度,而不是总是填充窗口高度。

通过使用SwipeRefreshLayoutListView 来包装内容(项目)。

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

我怎样才能做到这一点?

更新:

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

我用RecyclerView 替换了ListView 并且RecyclerView 是包装内容高度,这很好。但 SwipeRefreshLayout 仍然是 filling height 强制 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:

使用 RecyclerViewWrapping 它与 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 仍在填充高度。

【问题讨论】:

    标签: android listview swiperefreshlayout dialogfragment android-wrap-content


    【解决方案1】:

    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 中使用 &lt;com.yourpackage.WrapingHeightListView... 而不是当前的 &lt;ListView ... 标签

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

    【讨论】:

    • 扩展的 ListView 类可能有效,但我无法判断,因为 SwipeRefreshLayout 仍然填充整个窗口,而忽略了它的 wrap_content。这正常吗?我会看到 RecyclerView,但我希望我能坚持使用 ListView。也许 RecyclerView 本身也填充父项,因为 SwipeRefreshLayout 不会将 wrap_content 应用于自身!
    • 确认 SwipeRefreshLayout 是填充整个窗口的东西,删除它并且 RecyclerView wrap_content 工作。怎么处理 SwipeRefreshLayout 到 wrap_content 里面有 RecyclerView!
    • some answers 堆栈溢出建议将ListViewRecyclerView 包装在LinearLayout 中。他们正在确认您的错误行为来自SwipeRefreshLayout,因此可能不需要上面的子类WrapingHeightListView(仍然值得尝试)
    • 它们都不起作用..我总是看到SwipeRefreshLayout 正在填充高度而忽略wrap_content 这就是问题所在,RecyclerView 与 wrap_content 一起使用。
    • 您可以发布列表项 XML 吗?其他一些答案建议将wrap_content 也设置为对话框Window 像这样dialog.getWindow().setAttributes(lp) (lp = new WindowManager.LayoutParams)
    猜你喜欢
    • 2014-10-05
    • 2023-03-11
    • 1970-01-01
    • 1970-01-01
    • 2023-03-24
    • 1970-01-01
    • 1970-01-01
    • 2018-06-13
    • 2023-03-30
    相关资源
    最近更新 更多