如何将 SwipeRefreshLayout 添加到我的布局中?(没有 list/gridview)

How do I add SwipeRefreshLayout to my layout?(Without list/gridview)

我正在开发一个天气应用程序,我已经完成了网络(它可以搜索和显示任何城市的数据,也可以显示图标)。我正在尝试将 swiperefreshlayout 添加到 xml 片段中,以便我可以不时刷新应用程序并更新应用程序数据。但是当我检查它的官方文档时,即 https://developer.android.com/training/swipe/add-swipe-interface,他们说 “您可以通过将小部件作为 ListView 或 GridView 的父级添加到您的布局文件来启用此行为”。同时,我不使用其中任何一个,我只使用约束布局和 nestedscrollview,所以我尝试添加 SwipeRefreshLayout 即

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

作为我的约束布局的父级和子级,但它涵盖了整个 xml 布局,并且只有它在这两种情况下都反映出来。有没有可能将它添加到我的布局中? (没有 list/gridview)。如果有,请问我该怎么做?否则我应该遵循哪些其他步骤?

布局如下:

fragment_first.xml:

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

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".first.FirstFragment">

        <!-- TODO: Update blank fragment layout -->


        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            android:text="@string/current"
            android:textColor="#FF3D19"
            android:textSize="24sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.498"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <ImageView
            android:id="@+id/imageView3"
            android:layout_width="358dp"
            android:layout_height="337dp"
            android:contentDescription="@string/orange_panel"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/textView3"
            app:srcCompat="@drawable/orange_panel" />

        <TextView
            android:id="@+id/textView14"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="68dp"
            android:layout_marginTop="19dp"
            android:text="@string/sunrise"
            android:textColor="#0aff69"
            android:textSize="24sp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/imageView3" />

        <ImageView
            android:id="@+id/imageView11"
            android:layout_width="94dp"
            android:layout_height="40dp"
            android:layout_marginTop="19dp"
            android:contentDescription="@string/right_arrow"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.498"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/imageView3"
            app:srcCompat="@drawable/right_arrow"
            app:tint="#FFFFFF" />

        <TextView
            android:id="@+id/textView15"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="19dp"
            android:layout_marginEnd="76dp"
            android:text="@string/sunset"
            android:textColor="#ff0061"
            android:textSize="24sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/imageView3" />

        <ImageView
            android:id="@+id/imageView14"
            android:layout_width="146dp"
            android:layout_height="151dp"
            android:layout_marginStart="45dp"
            android:contentDescription="@string/rise_panel"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/imageView11"
            app:srcCompat="@drawable/rise_panel" />

        <ImageView
            android:id="@+id/imageView15"
            android:layout_width="146dp"
            android:layout_height="151dp"
            android:layout_marginEnd="44dp"
            android:contentDescription="@string/set_panel"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/imageView11"
            app:srcCompat="@drawable/set_panel" />

        <TextView
            android:id="@+id/textView16"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:text="@string/weather_conditions"
            android:textColor="#4230FF"
            android:textSize="24sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/imageView14" />

        <ImageView
            android:id="@+id/imageView16"
            android:layout_width="358dp"
            android:layout_height="337dp"
            android:layout_marginTop="4dp"
            android:contentDescription="@string/blue_panel"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/textView16"
            app:srcCompat="@drawable/blue_panel" />

        <TextView
            android:id="@+id/textView18"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_marginStart="53dp"
            android:layout_marginTop="33dp"
            android:text="@string/temperature"
            android:textColor="#FFFFFF"
            android:textSize="18sp"
            app:layout_constraintStart_toStartOf="@+id/imageView16"
            app:layout_constraintTop_toTopOf="@+id/imageView16" />

        <TextView
            android:id="@+id/textView20"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="53dp"
            android:layout_marginTop="24dp"
            android:text="@string/humidity"
            android:textColor="#FFFFFF"
            android:textSize="18sp"
            app:layout_constraintStart_toStartOf="@+id/imageView16"
            app:layout_constraintTop_toBottomOf="@+id/textView23" />

        <TextView
            android:id="@+id/textView21"
            android:layout_width="0dp"
            android:layout_height="24dp"
            android:layout_marginStart="53dp"
            android:layout_marginTop="24dp"
            android:text="@string/wind_speed"
            android:textColor="#FFFFFF"
            android:textSize="18sp"
            app:layout_constraintStart_toStartOf="@+id/imageView16"
            app:layout_constraintTop_toBottomOf="@+id/textView20" />

        <TextView
            android:id="@+id/textView22"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="53dp"
            android:layout_marginTop="24dp"
            android:text="@string/visibility"
            android:textColor="#FFFFFF"
            android:textSize="18sp"
            app:layout_constraintStart_toStartOf="@+id/imageView16"
            app:layout_constraintTop_toBottomOf="@+id/textView21" />

        <TextView
            android:id="@+id/textView23"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="53dp"
            android:layout_marginTop="24dp"
            android:text="@string/pressure"
            android:textColor="#FFFFFF"
            android:textSize="18sp"
            app:layout_constraintStart_toStartOf="@+id/imageView16"
            app:layout_constraintTop_toBottomOf="@+id/textView18" />

        <TextView
            android:id="@+id/textView24"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="53dp"
            android:layout_marginTop="24dp"
            android:text="@string/cloud"
            android:textColor="#FFFFFF"
            android:textSize="18sp"
            app:layout_constraintStart_toStartOf="@+id/imageView16"
            app:layout_constraintTop_toBottomOf="@+id/textView22" />

        <ImageView
            android:id="@+id/imageView6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="52dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="@+id/imageView3"
            app:srcCompat="@drawable/sun"
            android:contentDescription="@string/current_icon" />

        <TextView
            android:id="@+id/textView10"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:text="@string/current_temp"
            android:visibility="gone"
            android:textColor="#FFFFFF"
            android:textSize="30sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/imageView6" />

        <TextView
            android:id="@+id/textView11"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:text="@string/current_output"
            android:visibility="gone"
            android:textColor="#FFFFFF"
            android:textSize="24sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/textView10" />

        <ImageView
            android:id="@+id/imageView12"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="17dp"
            android:layout_marginTop="26dp"
            app:layout_constraintStart_toStartOf="@+id/imageView14"
            app:layout_constraintTop_toTopOf="@+id/imageView14"
            app:srcCompat="@drawable/small_sun"
            android:contentDescription="@string/rise_sun" />

        <TextView
            android:id="@+id/textView25"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="17dp"
            android:layout_marginBottom="31dp"
            android:text="@string/rise_time"
            android:visibility="gone"
            android:textColor="#FFFFFF"
            app:layout_constraintBottom_toBottomOf="@+id/imageView14"
            app:layout_constraintStart_toStartOf="@+id/imageView14" />

        <ImageView
            android:id="@+id/imageView13"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="17dp"
            android:layout_marginTop="26dp"
            app:layout_constraintStart_toStartOf="@+id/imageView15"
            app:layout_constraintTop_toTopOf="@+id/imageView15"
            app:srcCompat="@drawable/small_sun"
            android:contentDescription="@string/set_sun" />

        <TextView
            android:id="@+id/textView26"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="17dp"
            android:layout_marginBottom="31dp"
            android:text="@string/set_time"
            android:visibility="gone"
            android:textColor="#FFFFFF"
            app:layout_constraintBottom_toBottomOf="@+id/imageView15"
            app:layout_constraintStart_toStartOf="@+id/imageView15" />

        <TextView
            android:id="@+id/textView28"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="33dp"
            android:layout_marginEnd="53dp"
            android:text="@string/temp_out"
            android:visibility="gone"
            android:textColor="#FFFFFF"
            android:textSize="18sp"
            app:layout_constraintEnd_toEndOf="@+id/imageView16"
            app:layout_constraintTop_toTopOf="@+id/imageView16" />

        <TextView
            android:id="@+id/textView29"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="24dp"
            android:layout_marginEnd="53dp"
            android:text="@string/press_out"
            android:visibility="gone"
            android:textColor="#FFFFFF"
            android:textSize="18sp"
            app:layout_constraintEnd_toEndOf="@+id/imageView16"
            app:layout_constraintTop_toBottomOf="@+id/textView28" />

        <TextView
            android:id="@+id/textView30"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="24dp"
            android:layout_marginEnd="53dp"
            android:text="@string/humid_out"
            android:visibility="gone"
            android:textColor="#FFFFFF"
            android:textSize="18sp"
            app:layout_constraintEnd_toEndOf="@+id/imageView16"
            app:layout_constraintTop_toBottomOf="@+id/textView29" />

        <TextView
            android:id="@+id/textView33"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="24dp"
            android:layout_marginEnd="53dp"
            android:text="@string/ws_out"
            android:visibility="gone"
            android:textColor="#FFFFFF"
            android:textSize="18sp"
            app:layout_constraintEnd_toEndOf="@+id/imageView16"
            app:layout_constraintTop_toBottomOf="@+id/textView30" />

        <TextView
            android:id="@+id/textView34"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="24dp"
            android:layout_marginEnd="53dp"
            android:text="@string/visi_out"
            android:visibility="gone"
            android:textColor="#FFFFFF"
            android:textSize="18sp"
            app:layout_constraintEnd_toEndOf="@+id/imageView16"
            app:layout_constraintTop_toBottomOf="@+id/textView33" />

        <TextView
            android:id="@+id/textView35"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="24dp"
            android:layout_marginEnd="53dp"
            android:text="@string/cloud_out"
            android:visibility="gone"
            android:textColor="#FFFFFF"
            android:textSize="18sp"
            app:layout_constraintEnd_toEndOf="@+id/imageView16"
            app:layout_constraintTop_toBottomOf="@+id/textView34" />

    </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.core.widget.NestedScrollView>

我们可以在没有 RecyclerViewListViewGridView

的情况下添加 SwipeRefreshLayout

SwipeRefreshLayout主要用于刷新具有垂直手势功能的视图内容。

如果布局有滚动,则添加 SwipeRefreshLayout 作为 NestedScrollView 的父级,如果布局不可滚动,则只需添加 SwipeRefreshLayout 作为 Layout

的父级

例如 - 如果屏幕是可滚动的:

<?xml version="1.0" encoding="utf-8"?>
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".first.FirstFragment">
    <androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:fillViewport="true">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
             <!--Your sub views-->
        </androidx.constraintlayout.widget.ConstraintLayout>

    </androidx.core.widget.NestedScrollView>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

例如 - 如果屏幕不可滚动:

<?xml version="1.0" encoding="utf-8"?>
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".first.FirstFragment">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
             <!--Your sub views-->
        </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

如何停止刷新SwipeRefreshLayout:

您需要添加swipeRefreshLayout.setRefreshing(false);停止刷新

例如:我在 class 中添加了处理程序线程以停止刷新 SwipeRefreshLayout

    SwipeRefreshLayout realSwipe = findViewById(R.id.real_swipe);

    realSwipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            // perform you action here for ex. add refresh screen code here
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    // this code is for stop refreshing icon, After 1000 ms automatically refresh icon will stop
                    realSwipe.setRefreshing(false);
                }
            }, 1000);
        }
    });