Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Add SwipeToRefresh to RecyclerView Android

Have activity page with following code:

<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
        android:id="@+id/swipeRefreshLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#00FFFFFF"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/view"
        app:layout_constraintHorizontal_bias="0.0"
>

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/mainList"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#F0F2F5"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/view"
            app:layout_constraintVertical_bias="0"
            tools:layout_editor_absoluteX="0dp" />

    </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

On my main activity.kt file, I have the following code but I am unsure where i need to put listenings etc. as I didn’t write this part of the code;

package com.example.what2do_v2

import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.widget.ImageButton
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import com.example.what2do_v2.Adapter.TaskAdapter
import com.example.what2do_v2.Model.TaskResponse
import com.example.what2do_v2.Service.ApiClient
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response


class MainActivity : AppCompatActivity() {
    private val apiService by lazy {
        ApiClient.create()
    }


    private var taskData : ArrayList<TaskResponse> = arrayListOf();

    override fun onCreate(savedInstanceState: Bundle?) {

        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val searchbutton:ImageButton=findViewById(R.id.searchbutton)
        searchbutton.setOnClickListener{
            startActivity(Intent(this, ActivitySearch::class.java))
        }

        val saves:ImageButton=findViewById(R.id.saves)
        saves.setOnClickListener{
            startActivity(Intent(this, ActivitySaves::class.java))
        }

        val settings:ImageButton=findViewById(R.id.settings)
        settings.setOnClickListener{
            startActivity(Intent(this, ActivitySettings::class.java))
        }
        loadData();
    }

    private fun loadData(){
        var data = apiService.getMainData();

        data.enqueue(object : Callback<ArrayList<TaskResponse>> {

            override fun onResponse(
                call: Call<ArrayList<TaskResponse>>,
                response: Response<ArrayList<TaskResponse>>
            ) {

                if (response.code() == 200) {
                    taskData = response.body()!!;
                    displaydata();
                }

            }

            override fun onFailure(call: Call<ArrayList<TaskResponse>>, t: Throwable) {
                Log.d("-----------------", t.toString())
            }
        })
    }

    private fun displaydata(){
        var recyclerView = findViewById<RecyclerView>(R.id.mainList);
        var adapter = TaskAdapter(this, taskData);
        recyclerView.layoutManager = LinearLayoutManager(this)
        recyclerView.adapter = adapter

    }
}

I’m really struggling where to add additional "SwipeRefreshLayout" code into the above. I have tried to follow each example on stackflow, but can’t seem to understand where the appropriate places to add.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Initialize the SwipeRefreshLayout & assign a refresh listener in your onCreate() like this:

val swipeRefreshLayout: SwipeRefreshLayout = findViewById(R.id.swipeRefreshLayout)

// load data on swipe refresh.
swipeRefreshLayout.setOnRefreshListener { loadData() }

// Disable the refreshing after data load
swipeRefreshLayout.isRefreshing = false
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading