Aditya Dixit
Aditya Dixit

Reputation: 161

Swipe refresh layout not working with recyclerview

I have a layout with navigation view with drawer layout as parent. Then I have a linear layout for other views including swipe refresh layout and recycler view:

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout
    android:id="@+id/drawer_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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".activities.MainActivity"
    android:orientation="vertical"
    android:fitsSystemWindows="true">

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

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar_main_activity"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="@color/black"/>

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

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

        </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

    </LinearLayout>

    <com.google.android.material.navigation.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        app:headerLayout="@layout/nav_header_layout"
        app:menu="@menu/nav_main_layout"
        android:layout_gravity="start"/>

</androidx.drawerlayout.widget.DrawerLayout>

The problem is that the loading icon appears but never disappears even if I write code for it to disappear:

swipeRefreshLayout.setOnRefreshListener(object : SwipeRefreshLayout.OnRefreshListener{
            override fun onRefresh() {
                Toast.makeText(this@MainActivity, "Swiped", Toast.LENGTH_SHORT).show()
                swipeRefreshLayout.isRefreshing = false
            }

        })

Even the toast message doesn't appear.

What is the problem in my code or xml?

error

Upvotes: 1

Views: 4963

Answers (2)

nayan dhabarde
nayan dhabarde

Reputation: 2356

I have only faced these rendering issues when I had relative/constraint layout as parent of recycler view and the swipe refresh layout as parent of those layouts.

Change from


<SwipeRefreshLayout>
 <RelativeLayout>
   <RecyclerView>

to

<SwipeRefreshLayout>
   <RecyclerView>

This actually worked for me.

Upvotes: 1

Tanveer Munir
Tanveer Munir

Reputation: 1968

when you define swipeRefreshLayout as local into onCreate view it accesses only that block. if defined in the global swipeRefreshLayout it accesses anywhere in the class.

var swipeRefreshLayout: SwipeRefreshLayout? = null

In onCreate method

swipeRefreshLayout = binding.swipeRefreshLayout

For Calling

swipeRefreshLayout?.isRefreshing = false

Upvotes: 2

Related Questions