2

I tried to add Swipe-to-Refresh inside a fragment that contains a RecyclerView but after adding it the fragment becomes white. Tried with a simple textview and in fragment .setRefreshing(true) and still the fragment is white. If I move the Swipe to refresh layout to the activity everything works like a piece of charm. My questions is what am I doing wrong or why I can't have the Swipe to refresh inside the fragment?

My fragment XML:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">

    <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
        android:id="@+id/swipeToRefresh"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="test" />

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
</layout>

Activity XML:

<?xml version="1.0" encoding="utf-8"?>
<layout 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">

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".ui.main.MainActivity">
            <androidx.core.widget.NestedScrollView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:clipToPadding="false"
                android:paddingBottom="100dp">
                <FrameLayout
                    android:id="@+id/screenContainer"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent" />

            </androidx.core.widget.NestedScrollView>
        <!--            TODO REFACTOR COLORS -->
        <com.google.android.material.bottomappbar.BottomAppBar
            android:id="@+id/bottomAppBar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            app:backgroundTint="#000000"
            app:fabCradleMargin="10dp"
            app:hideOnScroll="true"
            app:menu="@menu/bottom_app_bar"
            app:navigationIcon="@drawable/ic_menu_24dp" />

        <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:id="@+id/floatingActionButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:backgroundTint="#e0f2f1"
            app:layout_anchor="@id/bottomAppBar"
            app:shapeAppearance="@style/FabDiamondOverlay"
            app:srcCompat="@drawable/ic_add_24dp" />

    </androidx.coordinatorlayout.widget.CoordinatorLayout>
</layout>

Gradle:

implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.1.0"

Emulator:

enter image description here

Uploaded a sample project on github.

https://github.com/tirlaovidiu/swipe-to-refresh-bug-example

Solved::

NestedScrollView replaced with a ConstraitLayout and the issue went away.

TOvidiu
  • 1,036
  • 11
  • 15

0 Answers0