如何制作SwipeRefreshLayout wrap_content?

时间:2019-02-12 11:26:56

标签: android android-layout android-recyclerview swiperefreshlayout

如何制作SwipeRefreshLayout wrap_content?

这是我的布局 mydialog_fragmet.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:id="@+id/contentLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/appBackgroundColorLight"
        android:orientation="vertical"
        >

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:clipToPadding="false"
            android:paddingTop="16dp"
            android:scrollbarSize="2dp"
            android:scrollbarStyle="outsideOverlay"
            android:scrollbarThumbVertical="@color/colorAccent"
            />

        <include layout="@layout/view_add_place_button"/>

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

结果是LinearLayout id/contentLayout变为match_parent。这是屏幕截图:

enter image description here

但是当我使用不带SwipeRefreshLayout的布局时,内容为wrap_content mydialog_fragmet.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/appBlue"
            android:orientation="vertical"
            >

            <android.support.v7.widget.RecyclerView
                android:id="@+id/placesRecyclerView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:clipToPadding="false"
                android:paddingTop="16dp"
                android:scrollbarSize="2dp"
                android:scrollbarStyle="outsideOverlay"
                android:scrollbarThumbVertical="@color/colorAccent"
                app:maxHeight="300dp"
                />

            <include layout="@layout/view_add_place_button"/>

        </LinearLayout>

这是正确的结果:

enter image description here

2 个答案:

答案 0 :(得分:0)

找到解决方案:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    >

    <com.example.views.MaxHeightSwipeRefreshLayout
        android:id="@+id/swipeRefreshLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:maxHeight="382dp">//size 382 = 300(RecyclerView) + 82 (addButton)

        <com.example.views.MaxHeightNestedScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:maxHeight="382dp">

            <LinearLayout
                xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:app="http://schemas.android.com/apk/res-auto"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/appBackgroundColorLight"
                android:orientation="vertical"
                tools:background="@color/appBlue"
                >

                <com.example.views.MaxHeightRecyclerView
                    android:id="@+id/placesRecyclerView"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:clipToPadding="false"
                    android:paddingTop="16dp"
                    android:scrollbarSize="2dp"
                    app:maxHeight="300dp"
                    android:scrollbarStyle="outsideOverlay"
                    android:scrollbarThumbVertical="@color/colorAccent"
                    />

                <include layout="@layout/view_add_place_button"/>

            </LinearLayout>
        </com.example.views.MaxHeightNestedScrollView>

    </com.example.views.MaxHeightSwipeRefreshLayout>

</LinearLayout>

MaxHeight*视图来自https://stackoverflow.com/a/48728490/2425851

答案 1 :(得分:0)

针对此特定行为的解决方案是将视图包装在父版式中,仅当您希望使用该尺寸时才建议使用特定的dp

<LinearLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/appBackgroundColorLight"
        android:orientation="vertical">

   <android.support.v4.widget.SwipeRefreshLayout
          android:layout_width="match_parent"
          android:layout_height="wrap_content">

    <LinearLayout
        android:id="@+id/contentLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/appBackgroundColorLight"
        android:orientation="vertical"
        >

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:clipToPadding="false"
            android:paddingTop="16dp"
            android:scrollbarSize="2dp"
            android:scrollbarStyle="outsideOverlay"
            android:scrollbarThumbVertical="@color/colorAccent"
            />

        <include layout="@layout/view_add_place_button"/>

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