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.
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.
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:
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.
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:
After adding these dependencies, remember to click on 'Sync Now'.
Proceed with the following tasks in your activity_main.xml file:
Add SwipeRefreshLayout and RecyclerView by inserting the provided code snippet:
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.
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> |
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; |
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); } } |