RecyclerView в SwipeRefreshLayout не "wrap_content"

У меня есть RecyclerView как единственный ребенок SwipeRefreshLayoutЯ хочу RecyclerView wrap_content, Когда я устанавливаю их обоих "wrap_content", это не работает. RecyclerView с меньшим количеством предметов также match_parent, Когда я удаляю SwipeRefreshLayout, RecyclerView будут wrap_content, Может кто-нибудь мне помочь? Мой английский плохой, Может быть, вы не можете понять. Кто-нибудь может мне помочь? Большое спасибо.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/container_v"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v4.widget.SwipeRefreshLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#00ffff">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#ff00ff">


    </android.support.v7.widget.RecyclerView>
</android.support.v4.widget.SwipeRefreshLayout>

4 ответа

Наконец, я решаю это. Из этого ответа

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/container_v"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v4.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">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recycler_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#ff00ff" />


    </LinearLayout>


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

Ключ в том, чтобы использовать Layout wrap RecyclerView. Из-за использования SwipeRefreshLayoutMeasureSpec.EXACTLY режим измерения mTarget size при onMeasure() :

    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        if (mTarget == null) {
            ensureTarget();
        }
        if (mTarget == null) {
            return;
        }
        mTarget.measure(MeasureSpec.makeMeasureSpec(
                getMeasuredWidth() - getPaddingLeft() - getPaddingRight(),
                MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(
                getMeasuredHeight() - getPaddingTop() - getPaddingBottom(), MeasureSpec.EXACTLY));
        ......
    }

А mTarget - это первое дочернее представление SwipeRefreshLayout. Таким образом, это приведет к тому, что mTarget всегда будет соответствовать родительскому объекту.

    private void ensureTarget() {
        // Don't bother getting the parent height if the parent hasn't been laid
        // out yet.
        if (mTarget == null) {
            for (int i = 0; i < getChildCount(); i++) {
                View child = getChildAt(i);
                if (!child.equals(mCircleView)) {
                    mTarget = child;
                    break;
                }
            }
        }
    }

FrameLayout используется потому, что он более легкий.

    <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <androidx.recyclerview.widget.RecyclerView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:paddingTop="10dp"
                android:paddingBottom="10dp"
                tools:itemCount="2"
                tools:listitem="@layout/item_msg_detail_text_style" />

        </FrameLayout>

    </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

Это сработало для меня с AndroidX

      <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
    android:id="@+id/swiperefresh"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <androidx.recyclerview.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clipToPadding="false"
        android:paddingBottom="50dp"
        android:id="@+id/rooms">


    </androidx.recyclerview.widget.RecyclerView>
    </androidx.core.widget.NestedScrollView>



</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container_v"
    android:layout_width="match_parent"
    android:weightSum="1"
    android:orientation="vertical"
    android:layout_height="match_parent"
    tools:context=".classes.Splash">
    <android.support.v4.widget.SwipeRefreshLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight=".5"
        android:background="#00ffff">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recycler_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#ff00ff">


        </android.support.v7.widget.RecyclerView>
    </android.support.v4.widget.SwipeRefreshLayout>

Вы можете попробовать придать вес линейному расположению. Это будет работать