RecyclerView ci permette di ragruppare diversi elementi e visualizzarli in un elenco scorrevole verticale. In questo esempio andiamo a specificare un adattatore per collegare i dati a RecyclerView, un gestore di layout per gestire la disposizione degli elementi dell’ elenco e un oggetto holder per definire il modo in cui vengono visualizzati i dati:
activity_main.xml
<?xml version=”1.0″ encoding=”utf-8″?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android=”http://schemas.android.com/apk/res/android”
xmlns:app=”http://schemas.android.com/apk/res-auto”
xmlns:tools=”http://schemas.android.com/tools”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
tools:context=”.MainActivity”>
<androidx.recyclerview.widget.RecyclerView
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:id=”@+id/recyclerView”/>
</androidx.constraintlayout.widget.ConstraintLayout>
recycler_list.xml
<com.google.android.material.card.MaterialCardView xmlns:android=”http://schemas.android.com/apk/res/android”
xmlns:app=”http://schemas.android.com/apk/res-auto”
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>
RecyclerViewAdapter.java
package com.example.recyclerView;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyHolder >
{
Context context;
ArrayList<Pojo>arrayList;
public RecyclerViewAdapter(Context context,ArrayList<Pojo> arrayList){
this.context=context;
this.arrayList=arrayList;
}
@Override
public RecyclerViewAdapter.MyHolder onCreateViewHolder(ViewGroup parent,
int viewType){
View view=LayoutInflater.from(context).inflate(R.layout.recycler_list,parent,false);
return new MyHolder (view);
}
@Override
public void onBindViewHolder(RecyclerViewAdapter.MyHolder
holder,int position){
holder.colorName.setText(arrayList.get(position).getColor());
holder.colorCode.setText(arrayList.get(position).getCode());
}
@Override
public int getItemCount(){
return arrayList.size();
}
public class MyHolder extends RecyclerView.ViewHolder{
TextView colorName,colorCode;
public MyHolder(View view){
super(view);
colorName=view.findViewById(R.id.name);
colorCode=view.findViewById(R.id.code);
}
}
}
Pojo.java
package com.example.recyclerView;
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.recyclerView;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
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);
LinearLayoutManager linearLayoutManager=new LinearLayoutManager(this);
recyclerView.setLayoutManager(linearLayoutManager);
recyclerViewAdapter=new RecyclerViewAdapter(this,arrayList);
recyclerView.setAdapter(recyclerViewAdapter);
}
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”));
}
}