Explorer les Livres électroniques
Catégories
Explorer les Livres audio
Catégories
Explorer les Magazines
Catégories
Explorer les Documents
Catégories
Somaire
SaveInstanceState
Mémoire interne (tel ou sd)
Shared Preferences
SQLite
API (Firebase, Aws, server perso,...)
Types de donnés :
API 28 :
implementation 'com.android.support:recyclerview-v7:28.0.0‘
API 29 et suppérieur :
implementation 'androidx.recyclerview:recyclerview:1.1.0'
RecyclerView
Création de la vue recyclerview_main.xml
API 28:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/rvTodo"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
</android.support.v7.widget.RecyclerView>
API 29:
<?xml version="1.0" encoding="utf-8"?>
<androidx.recyclerview.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/rvTodo"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
</androidx.recyclerview.widget.RecyclerView>
RecyclerView
todo_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp">
<TextView
android:id="@+id/tvName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="1dp"
tools:text="Todo item" />
<TextView
android:id="@+id/tvUrgency"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="1dp"
tools:text="urgency"/>
</LinearLayout>
Organisation
Item
RecyclerView
Création d’un adaptateur
Class TodoAdapter
Class TodoAdapter
Class TodoAdapter
Explication
List<Todo> todos :
Stock la liste de Todos contenu l’adapter
TodoViewHolder
Classe interne qui représente la vue pour un item (cf .
Todo_item.xml)
onCreateViewHolder
Créé la view pour l’item (R.layout.todo_item)
onBindViewHolder
Bind un todo (objet Todo) avec les élément de la view item (tvName
et tvUrgency)
getIemCount
Retourne le nombre d’élément contenu dans la liste de l’adapter
(todos)
Problématique :
Serializable vs Parcelable
Performances
Passage d'un ArrayList dans un bundle
Exercice : rendre l'objet Todo Parcelable
http://tutos-android-france.com/passer-des-donnees-
entre-activites/
Pojo Todo
Pojo Todo
Modification de MainActivity.java
Les propriétés
onCreate
Explications
recyclerView = findViewById(R.id.rvTodo);
On récupère le RecyclerView de la vue
recyclerView.setLayoutManager(layoutManager);
On branche le layout manager à l’objet recyclerView
recyclerView.setAdapter(todoAdapter);
On branche l’adaptater à l’objet recyclerView
Sauvegarde des données
AsynTask