Home > front end >  How to cadd a swipe to refresh to my code?
How to cadd a swipe to refresh to my code?

Time:01-22

I have the following layout code, to call my recycler view

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".fragments.Status">

    <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
        android:id="@ id/swipeToRefresh"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintTop_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent">

        <androidx.recyclerview.widget.RecyclerView
            android:id="@ id/statusRecyclerView"
            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.swiperefreshlayout.widget.SwipeRefreshLayout>

I tried to call this swipe to refresh using this in my main activity, but just give me a bunch of errors, anyone know how to correctly implement the code below to really works in my app, I'm new into this and I may had missed some details

   /*
     * Sets up a SwipeRefreshLayout.OnRefreshListener that is invoked when the user
     * performs a swipe-to-refresh gesture.
     */
    mySwipeRefreshLayout.setOnRefreshListener {
        Log.i(LOG_TAG, "onRefresh called from SwipeRefreshLayout")

        // This method performs the actual data-refresh operation.
        // The method calls setRefreshing(false) when it's finished.
        myUpdateOperation()
    }

CodePudding user response:

Which errors does it throws? Anyway try with this solution:

mySwipeRefreshLayout.setOnRefreshListener(object : SwipeRefreshLayout.OnRefreshListener{
    override fun onRefresh() {
        Log.i(LOG_TAG, "onRefresh called from SwipeRefreshLayout")
        // This method performs the actual data-refresh operation.
        // The method calls setRefreshing(false) when it's finished.
        myUpdateOperation()
        mySwipeRefreshLayout.isRefreshing = false
    }
})

CodePudding user response:

Maybe you can write that

<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

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

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


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

</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

</androidx.appcompat.widget.LinearLayoutCompat>

  •  Tags:  
  • Related