swipe to refresh layout

Using swipe-to-refresh layout in Android

Android provides a widget that implements the swipe to refresh layout mode, and users can use it to trigger vertical sliding updates.

This is achieved through the SwipeRefreshLayout widget, which detects vertical movement, displays a prominent progress bar, and triggers callback methods in the application.

This ViewGroup is extremely useful because it eliminates the need to define a custom layout that detects a swipe gesture and then performs a Swipe refresh action on it. With the help of an example app, we’ll see how the SwipeRefreshLayout is used in Android.
Note: It should be noted that the SwipeRefreshLayout should act as the parent and can only have one direct child that will perform the refresh action on it.

There are only a few methods:

  • addOnRefreshListener(OnRefreshListener): creates a listener to notify other portions of the code when refreshing starts.
  • setRefreshing(boolean): toggles the visibility of progress.
  • isRefreshing(): determines whether the view needs to be refreshed.

Preview

<– Swipe to refresh Example

dependency:

//implementation for swipe-to-refresh layout
   
 implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.1.0"

File: MainActivity.kt

–> In MainActivity.kt file initializes variable and implements handler method.

package com.example.refreshlayout

import android.os.Bundle
import android.os.Handler
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        var number=0
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        title="Swipe to Refresh Layout"
        var swipeRefreshLayout=findViewById<SwipeRefreshLayout>(R.id.swipe)
        var textView=findViewById<TextView>(R.id.textView)
        swipeRefreshLayout.setOnRefreshListener{
            number++
            textView.text = "Total number =$number"
            Handler().postDelayed(
                Runnable {
                    swipeRefreshLayout.isRefreshing=false
                },1000

            )

        }
    }
}

File: activity_main.xml

–> In activity_main.xml file add swiperefreshlayout.widget and textview .

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/teal_200"
    tools:context=".MainActivity">
    <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
        android:id="@+id/swipelayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="Swipe to Reload"
            android:textColor="@android:color/background_dark"
            android:textSize="24sp"
            android:textStyle="bold" />
    </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
</RelativeLayout>
  • We hope that this guide will assist you in quickly creating a simple swipe refresh layout Android app. If you have any problems, please post them in the comments section and we will gladly assist you.

http://theudaipurstore.com

This Post Has One Comment

Leave a Reply