Software Development Exam  >  Software Development Notes  >  Pull to Refresh with RecyclerView in Android with Example

Pull to Refresh with RecyclerView in Android with Example - Software Development PDF Download

  • Pull to Refresh with RecyclerView in Android with Example

  • Last Updated: 01 Oct, 2021
  • Introduction to SwipeRefreshLayout Widget

    The SwipeRefreshLayout widget is utilized to implement a swipe-to-refresh user interface design pattern in Android applications. Users can refresh the content of views by performing a vertical swipe gesture. This widget detects the vertical swipe, displays a progress bar, and triggers callback methods within the app. Applications like Gmail, YouTube, Facebook, and Instagram make use of this Material Design UI pattern to enable manual refreshing of content.

  • SwipeRefreshLayout Class and OnRefreshListener

    The SwipeRefreshLayout class includes a listener known as OnRefreshListener. Classes intending to utilize this listener must implement the SwipeRefreshLayout.OnRefreshListener interface. When a vertical swipe-down gesture is detected, the onRefresh() method is triggered. It can be overridden as needed to handle the refresh behavior.

  • Implementation Example

    In the following example, data will be stored in an ArrayList to populate a RecyclerView. Upon calling the onRefresh() method, the data in the ArrayList will be reorganized. The project will be implemented using the Java language. Below is a sample GIF illustrating the functionality:

    Pull to Refresh with RecyclerView in Android with Example - Software Development
  • Step by Step Implementation

    • Step 1: Create a New Project

      To initiate a new project in Android Studio, follow these steps:

      Start by creating a new project in Android Studio. Ensure that you choose Java as the programming language.

    • Step 2: Adding dependencies

      Now, let's add dependencies for RecyclerView and SwipeRefreshLayout:

      In your Android Studio project, navigate to Gradle Scripts > build.gradle(Module: app) and incorporate the following dependencies:

      • implementation "androidx.recyclerview:recyclerview:1.1.0"
      • implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.1.0"

      After adding these dependencies, remember to click on 'Sync Now'.

    • Step 3: Working with the activity_main.xml file

      Proceed with the following tasks in your activity_main.xml file:

      Add SwipeRefreshLayout and RecyclerView by inserting the provided code snippet:

Creating RecyclerView List Items

  • Step 4: Create a new layout file list_item.xml for the list items of RecyclerView

    Within your Android project, navigate to the following path: app > res > layout. Right-click, go to New, and select Layout Resource File. Name this file as list_item. This layout file (list_item.xml) will comprise an ImageView and a TextView, essential for populating the rows of the RecyclerView.

  • Contents of list_item.xml File

    The list_item.xml file includes a LinearLayout containing an ImageView and a TextView. The ImageView is set to display an image (ic_launcher) while the TextView shows text ("GeeksForGeeks" in this case).

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="10dp" android:orientation="horizontal" android:padding="10dp">
    <ImageView android:id="@id/imageView" android:layout_width="120dp" android:layout_height="120dp" android:scaleType="fitXY" android:src="https://cn.edurev.in/mipmap/ic_launcher" />
    <TextView android:id="@id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:paddingStart="10dp" android:text="GeeksForGeeks" />
    </LinearLayout>

Step 5: Creating Adapter class for RecyclerView

  • Create a new Java class named Adapter in the specified package. This class will extend RecyclerView.Adapter with ViewHolder.
  • In the Adapter class, you will override the onCreateViewHolder() method. This method inflates the list_item.xml layout and passes it to the ViewHolder.
  • Next, you will implement the onBindViewHolder() method in the Adapter class. This method is responsible for setting data to the Views using the ViewHolder.
  • Below is a summarized code snippet for the Adapter.java class:

Adapter.java class

  • The Adapter class extends RecyclerView.Adapter and implements necessary methods.
  • It contains constructors for initializing context, images, and text.
  • The onCreateViewHolder() method inflates the list_item.xml layout file into a View object.
  • The onBindViewHolder() method binds data to specified positions.

Code Snippet

import android.content.Context;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;
import android.widget.ImageView;import android.widget.TextView;import androidx.annotation.NonNull;import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
  • The ViewHolder class initializes views for images and text.
  • The Adapter class is responsible for managing data and binding it to the RecyclerView.
  • It ensures that the correct data is displayed in the appropriate positions within the RecyclerView.

Working with MainActivity.java file

  • Creating ArrayLists in MainActivity.java to store images and text data.
  • Images are stored in the drawable folder, and references to SwipeRefreshLayout and RecyclerView are obtained.
  • Setting the LayoutManager and Adapter for RecyclerView to display items and implementing onRefreshListener.

MainActivity.java Code Snippet

import android.os.Bundle; import androidx.appcompat.app.AppCompatActivity; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.swiperefreshlayout.widget.SwipeRefreshLayout; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Random; public class MainActivity extends AppCompatActivity {   SwipeRefreshLayout swipeRefreshLayout;   RecyclerView recyclerView;   // Using ArrayList to store images and text data   ArrayList images = new ArrayList< />(Arrays.asList(R.drawable.facebook, R.drawable.twitter, R.drawable.instagram, R.drawable.linkedin, R.drawable.youtube, R.drawable.whatsapp));   ArrayList text = new ArrayList< />(Arrays.asList("Facebook", "Twitter", "Instagram", "LinkedIn", "Youtube", "WhatsApp"));   @Override   protected void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     setContentView(R.layout.activity_main);     swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout);     recyclerView = (RecyclerView) findViewById(R.id.recyclerView);     LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getApplicationContext());     recyclerView.setLayoutManager(linearLayoutManager);     Adapter adapter = new Adapter(MainActivity.this, images, text);     recyclerView.setAdapter(adapter);     swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {       @Override       public void onRefresh() {         swipeRefreshLayout.setRefreshing(false);         RearrangeItems();       }     });   }   public void RearrangeItems() {     Collections.shuffle(images, new Random(System.currentTimeMillis()));     Collections.shuffle(text, new Random(System.currentTimeMillis()));     Adapter adapter = new Adapter(MainActivity.this, images, text);     recyclerView.setAdapter(adapter);   } }

Output:

  • Video Player
  • 00:00
  • 00:13
  • Use Up/Down Arrow keys to increase or decrease volume.

Please Login to comment...

  • Login
  • Like
The document Pull to Refresh with RecyclerView in Android with Example - Software Development is a part of Software Development category.
All you need of Software Development at this link: Software Development
Download as PDF

Top Courses for Software Development

Related Searches

Viva Questions

,

pdf

,

practice quizzes

,

Objective type Questions

,

Free

,

Important questions

,

Sample Paper

,

Pull to Refresh with RecyclerView in Android with Example - Software Development

,

video lectures

,

shortcuts and tricks

,

Pull to Refresh with RecyclerView in Android with Example - Software Development

,

Summary

,

Exam

,

Extra Questions

,

mock tests for examination

,

Previous Year Questions with Solutions

,

MCQs

,

ppt

,

past year papers

,

study material

,

Pull to Refresh with RecyclerView in Android with Example - Software Development

,

Semester Notes

;