SwipeRefreshLayout

SwipeRefreshLayout viene utilizzato per aggiornare il contenuto di una vista tramite un gesto di scorrimento verticale che, invocando il metodo onRefresh(), visualizza una bara di avanzamento. Una volta recuperati i dati, l’indicatore viene nascosto chiamando setRefreshing(false).
Qui un esempio:

activity_main.xml
<?xml version=”1.0″ encoding=”utf-8″?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
    xmlns:app=”http://schemas.android.com/apk/res-auto&#8221;
    xmlns:tools=”http://schemas.android.com/tools&#8221;
    android:layout_width=”match_parent”
    android:layout_height=”match_parent”
    tools:context=”.MainActivity”>

    <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
        android:layout_width=”match_parent”
        android:layout_height=”match_parent”
android:id=”@+id/swipeRefresh”>

    <androidx.recyclerview.widget.RecyclerView
        android:layout_width=”match_parent”
        android:layout_height=”match_parent”
android:id=”@+id/recyclerView”/>

</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

recycler_list.xml
<com.google.android.material.card.MaterialCardView xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
    xmlns:app=”http://schemas.android.com/apk/res-auto&#8221;
    app:cardBackgroundColor=”#E0F7DA”
app:cardElevation=”20dp”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
app:cardCornerRadius=”40dp”
app:contentPadding=”30dp”
android:layout_margin=”20dp”
android:clickable=”true”
android:foreground=”?android:attr/selectableItemBackground”>

<LinearLayout
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:orientation=”vertical”>

<TextView
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:id=”@+id/name”
android:textSize=”20sp”
android:padding=”5dp”/>

<TextView
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:id=”@+id/code”
android:textSize=”20sp”
android:padding=”5dp”/>

</LinearLayout>
</com.google.android.material.card.MaterialCardView>

colorName=view.findViewById(R.id.name);
colorCode=view.findViewById(R.id.code);

}
}
}

Pojo.java
package com.example.swipeRefreshLayout;

public class Pojo {
private String colorName;
private String colorCode;
public Pojo(String colorName,String coloCode){

this.colorName=colorName;
this.colorCode=coloCode;

}
public String getColor(){
return colorName;
}
public String getCode(){
return colorCode;
}
}

MainActivity.java
package com.example.swipeRefreshLayout;

import android.graphics.Color;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

SwipeRefreshLayout swipeRefreshLayout;
RecyclerView recyclerView;
ArrayList<Pojo>arrayList;
RecyclerViewAdapter recyclerViewAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

listColor();

recyclerView=findViewById(R.id.recyclerView);
swipeRefreshLayout=findViewById(R.id.swipeRefresh);

LinearLayoutManager linearLayoutManager=new LinearLayoutManager(this);
recyclerView.setLayoutManager(linearLayoutManager);
recyclerViewAdapter=new RecyclerViewAdapter(this,arrayList);

recyclerView.setAdapter(recyclerViewAdapter);

swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener(){
@Override
public void onRefresh(){
refreshListColor();
}
});

}
private void refreshListColor(){
recyclerView.postDelayed(new Runnable(){
@Override
public void run(){
recyclerView.smoothScrollToPosition(recyclerViewAdapter.getItemCount()-1);
  swipeRefreshLayout.setRefreshing(false);
  Toast.makeText(MainActivity.this,”ListColor Refreshed”,Toast.LENGTH_SHORT).show();
 
}
},3000);
}
private void listColor(){
arrayList=new ArrayList<Pojo>();

arrayList.add(new Pojo(“Red”,”#FF0000″));
arrayList.add(new Pojo(“Yellow”,”#FFFF00″));
arrayList.add(new Pojo(“Blue”,”#0000FF”));
arrayList.add(new Pojo(“Green”,”#008000″));
arrayList.add(new Pojo(“Orange”,”#FFA500″));
arrayList.add(new Pojo(“Purple”,”#EE82EE”));
    }
}