【问题标题】:RecyclerView in SwipeRefreshLayout not "wrap_content"SwipeRefreshLayout 中的 RecyclerView 不是“wrap_content”
【发布时间】:2023-03-11 07:26:01
【问题描述】:

我有一个RecyclerView 作为SwipeRefreshLayout 的唯一孩子,我想要RecyclerView wrap_content。当我将它们都设置为“wrap_content”时,它不起作用。项目较少的RecyclerView 也是match_parent。当我删除SwipeRefreshLayout 时,RecyclerViewwrap_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>

【问题讨论】:

  • 请发布 xml 代码。也可以尝试 wrap_content 的 SwipeRefreshLayout
  • 我已经将SwipeRefreshLayout设置为wrap_content,但是RecyclerView也是match_parent

标签: android android-recyclerview swiperefreshlayout android-wrap-content


【解决方案1】:

最后,我解决了。来自this Answer

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

【讨论】:

  • 现在我的 ScrollView 开始在中间刷新的问题不仅是在到达顶部时,因为它应该,因为 LinearLayout
【解决方案2】:

关键是使用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 总是匹配 parent。

    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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-26
    • 1970-01-01
    • 2015-05-06
    • 1970-01-01
    • 2014-09-30
    • 1970-01-01
    • 2017-10-26
    • 1970-01-01
    相关资源
    最近更新 更多