【问题标题】:RecyclerView inside SwipeRefreshLayout does not showSwipeRefreshLayout 内的 RecyclerView 不显示
【发布时间】:2016-10-20 08:39:15
【问题描述】:

我在滚动视图中使用 RecyclerView 实现了一个水平滚动项列表视图。我想在用户滑动到列表视图末尾时更新项目(刷新后添加项目)。以下代码是布局实现。

<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

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

            <android.support.v7.widget.RecyclerView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                app:layoutManager="android.support.v7.widget.LinearLayoutManager">

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

    </LinearLayout>

</ScrollView>

在我添加 SwipeRefreshLayout 之前,一切正常(RecyclerView 中的项目正常显示),但在我添加 SwipeRefreshLayout 之后,RecyclerView 消失了(在设备中和在 Android Studio 中预览)

您能找出解决此问题的方法吗?提前致谢!!

编辑 这是我想做的

【问题讨论】:

  • 将 SwipeRefreshLayout 添加到父级并移除滚动视图

标签: android android-layout android-recyclerview swiperefreshlayout recyclerview-layout


【解决方案1】:

试试这个,

<?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="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin">

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

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <android.support.v7.widget.RecyclerView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                app:layoutManager="android.support.v7.widget.LinearLayoutManager" />

            <android.support.v7.widget.RecyclerView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                app:layoutManager="android.support.v7.widget.LinearLayoutManager" />

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

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

【讨论】:

  • 主布局应该是可滚动的。其实ScrollView里面有很多item。
  • 让 swiperefresh 布局为父布局,并在该线性布局内添加嵌套滚动视图并将您的项目放在那里。
  • 同一个页面有多个 RecyclerView 和 SwipeRefresh 怎么办?也许我需要添加一些屏幕截图以使问题更清楚。
  • 检查编辑后的答案。你可以像这样添加recyclerview的数量。但是,如果您想单独刷新每个回收站视图,我建议您在屏幕截图的灰色部分设置按钮。
  • 这就是我要找的。谢谢。
【解决方案2】:

ScrollView 中使用SwipeRefreshLayout 会产生问题

使用这个为我工作。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

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

                <androidx.recyclerview.widget.RecyclerView
                    android:id="@+id/recyclerView"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layoutAnimation="@anim/layout_animation"
                    android:nestedScrollingEnabled="false">
                </androidx.recyclerview.widget.RecyclerView>

            </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

</RelativeLayout>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-06-17
    • 2018-07-20
    • 2023-03-11
    • 2014-09-30
    • 2016-10-24
    • 1970-01-01
    • 2015-10-31
    • 1970-01-01
    相关资源
    最近更新 更多