【问题标题】:How to make a view scrollable which contains swipe refresh layout?如何使包含滑动刷新布局的视图可滚动?
【发布时间】:2019-07-25 13:03:00
【问题描述】:

我正在尝试使包含滑动刷新布局的视图可滚动。我的xml在这里

<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    android:scrollbars="none"
    xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <androidx.cardview.widget.CardView
        android:id="@+id/post"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <include layout="@layout/layout_post" />
    </androidx.cardview.widget.CardView>

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

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/comment_recycler_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
</LinearLayout>
</androidx.core.widget.NestedScrollView>

但它不显示recycler-view 的内容。只显示Cardview的内容 不使用NestedScrollView 它显示如下。它显示了cardivew 和recycler 视图的内容。但我想让我的整个屏幕可滚动。我做错了什么?

【问题讨论】:

  • 为什么要使用nestedscrollview?
  • @SumitShukla 我是 Android 的菜鸟。但是使用scroll view 给我的输出与NestedScrollView 相同。

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


【解决方案1】:

SwipeRefreshLayout 将占据整个高度,您需要将其添加为您的 xml 的父级:

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

    <androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true"
        android:scrollbars="none">

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

            <androidx.cardview.widget.CardView
                android:id="@+id/post"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

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

            </androidx.cardview.widget.CardView>

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/comment_recycler_view"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
        </LinearLayout>

    </androidx.core.widget.NestedScrollView>

</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

【讨论】:

  • 但它显示为我的第一张图片。 recycler view 的内容没有显示?
  • 我不确定你到底发生了什么,但请确保你设置了 recyclerView.isNestedScrollingEnabled = false
  • @ShahrearBinAmin
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-09-30
  • 1970-01-01
  • 2013-04-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-31
相关资源
最近更新 更多