SwipeRefreshLayout中的RecyclerView不是“wrap_content”

时间:2016-12-03 06:41:56

标签: android android-recyclerview swiperefreshlayout android-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> 。谁能帮我?我的英语很差,也许你无法理解。有人可以帮帮我吗?非常感谢你。

ForEach ($Name In $NewTabNames){
    $Break++
    $Excel.Worksheets($Break).Name = $Name

    $Excel.Worksheets($Break).Cells(1,1).Font.Bold = $true
    $Excel.Worksheets($Break).Cells(1,1) = "Hostname"
    $Excel.Worksheets($Break).Cells(1,2).Font.Bold = $true
    $Excel.Worksheets($Break).Cells(1,2) = "IP Address"

1 个答案:

答案 0 :(得分:0)

关键是使用Layout wrap RecyclerView。由于SwipeRefreshLayout使用MeasureSpec.EXACTLY模式,因此在onMeasure()时测量mTarget大小:

    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>