From ecc5c6346aa5a9cb1819e6f51a58ad85fde265bc Mon Sep 17 00:00:00 2001
From: Thomas Delcroix <thomas.delcroix2.etu@univ-lille.fr>
Date: Thu, 12 Jan 2023 01:39:48 +0100
Subject: [PATCH] =?UTF-8?q?Affichage=20des=20donn=C3=A9es=20que=20l'on=20r?=
=?UTF-8?q?=C3=A9cup=C3=A8re=20dans=20le=20recyclerView?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
app/build.gradle | 3 ++
.../java/fr/delcroix_dupont/HomeAdapter.java | 30 ++++++++++++++++
.../fr/delcroix_dupont/HomeGridAdapter.java | 30 ++++++++++++----
.../fr/delcroix_dupont/HomeListAdapter.java | 35 +++++++++++++------
.../java/fr/delcroix_dupont/MainActivity.java | 1 +
.../java/fr/delcroix_dupont/MyViewHolder.java | 5 +++
.../fr/delcroix_dupont/api/post/Data__1.java | 11 ++++++
.../fr/delcroix_dupont/api/user/Data.java | 3 ++
.../delcroix_dupont/api/user/RedditUser.java | 3 ++
.../delcroix_dupont/ui/home/HomeFragment.java | 21 ++++++-----
10 files changed, 116 insertions(+), 26 deletions(-)
create mode 100644 app/src/main/java/fr/delcroix_dupont/HomeAdapter.java
diff --git a/app/build.gradle b/app/build.gradle
index d166299..aeb8dc9 100644
--- a/app/build.gradle
+++ b/app/build.gradle
@@ -61,4 +61,7 @@ dependencies {
implementation 'io.reactivex.rxjava2:rxjava:2.2.21'
implementation 'io.reactivex.rxjava2:rxandroid:2.1.1'
+ // Glide
+ implementation 'com.github.bumptech.glide:glide:4.11.0'
+
}
\ No newline at end of file
diff --git a/app/src/main/java/fr/delcroix_dupont/HomeAdapter.java b/app/src/main/java/fr/delcroix_dupont/HomeAdapter.java
new file mode 100644
index 0000000..e2b949a
--- /dev/null
+++ b/app/src/main/java/fr/delcroix_dupont/HomeAdapter.java
@@ -0,0 +1,30 @@
+package fr.delcroix_dupont;
+
+import androidx.recyclerview.widget.RecyclerView;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import fr.delcroix_dupont.api.post.Child;
+
+public abstract class HomeAdapter extends RecyclerView.Adapter<MyViewHolder>{
+
+ protected List<Child> posts;
+
+ public HomeAdapter(List<Child> posts){
+ this.posts = posts;
+ }
+
+ public HomeAdapter(){
+ this.posts = new ArrayList<Child>();
+ }
+
+ public void setRedditPostData(List<Child> posts) {
+ this.posts = posts;
+ notifyDataSetChanged();
+ }
+
+ public List<Child> getPosts(){
+ return this.posts;
+ }
+}
diff --git a/app/src/main/java/fr/delcroix_dupont/HomeGridAdapter.java b/app/src/main/java/fr/delcroix_dupont/HomeGridAdapter.java
index 95434a2..238218e 100644
--- a/app/src/main/java/fr/delcroix_dupont/HomeGridAdapter.java
+++ b/app/src/main/java/fr/delcroix_dupont/HomeGridAdapter.java
@@ -5,14 +5,25 @@ import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
-import androidx.recyclerview.widget.RecyclerView;
-public class HomeGridAdapter extends RecyclerView.Adapter<MyViewHolder> {
- private String[] authors;
+import com.bumptech.glide.Glide;
+import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
+
+import java.util.List;
+
+import fr.delcroix_dupont.api.post.Child;
+
+public class HomeGridAdapter extends HomeAdapter {
private MainActivity main;
- public HomeGridAdapter(MainActivity main, String[] authors){
- this.main = main; this.authors = authors;
+ public HomeGridAdapter(MainActivity main, List<Child> posts){
+ super(posts);
+ this.main = main;
+ }
+
+ public HomeGridAdapter(MainActivity main){
+ super();
+ this.main = main;
}
@NonNull
@@ -26,12 +37,17 @@ public class HomeGridAdapter extends RecyclerView.Adapter<MyViewHolder> {
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
- holder.getAuthorButton().setText(authors[position]);
+ holder.getAuthorButton().setText(this.posts.get(position).getData().getAuthor());
+ Glide.with(main)
+ .load(this.posts.get(position).getData().getThumbnail())
+ .centerCrop()
+ .transition(DrawableTransitionOptions.withCrossFade())
+ .into(holder.getImg());
}
@Override
public int getItemCount() {
- return authors.length;
+ return this.posts.size();
}
}
diff --git a/app/src/main/java/fr/delcroix_dupont/HomeListAdapter.java b/app/src/main/java/fr/delcroix_dupont/HomeListAdapter.java
index e0b4054..fd4d66a 100644
--- a/app/src/main/java/fr/delcroix_dupont/HomeListAdapter.java
+++ b/app/src/main/java/fr/delcroix_dupont/HomeListAdapter.java
@@ -1,23 +1,31 @@
package fr.delcroix_dupont;
-import androidx.recyclerview.widget.RecyclerView;
import android.view.ViewGroup;
import android.view.*;
import androidx.annotation.NonNull;
+import java.util.List;
+
+import fr.delcroix_dupont.api.post.Child;
import fr.delcroix_dupont.ui.home.author.FragmentAuthor;
-public class HomeListAdapter extends RecyclerView.Adapter<MyViewHolder> {
+import com.bumptech.glide.Glide;
+import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
+
+public class HomeListAdapter extends HomeAdapter {
private MainActivity main;
private FragmentAuthor fragmentAuthor;
- private String[] authors, upvotes, downvotes;
- public HomeListAdapter(MainActivity main, String[] authors, String[] upvotes, String[] downvotes){
+ public HomeListAdapter(MainActivity main, List<Child> posts){
+ super(posts);
+ this.main = main;
+ fragmentAuthor = null;
+ }
+
+ public HomeListAdapter(MainActivity main){
+ super();
this.main = main;
- this.authors = authors;
- this.upvotes = upvotes;
- this.downvotes = downvotes;
fragmentAuthor = null;
}
@@ -32,14 +40,19 @@ public class HomeListAdapter extends RecyclerView.Adapter<MyViewHolder> {
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
- holder.getAuthorButton().setText(authors[position]);
- holder.getUpvotes().setText(upvotes[position]);
- holder.getDownvotes().setText(downvotes[position]);
+ holder.getAuthorButton().setText(this.posts.get(position).getData().getAuthor());
+ holder.getUpvotes().setText(this.posts.get(position).getData().getUps().toString());
+ holder.getDownvotes().setText(this.posts.get(position).getData().getDowns().toString());
+ Glide.with(main)
+ .load(this.posts.get(position).getData().getThumbnail())
+ .centerCrop()
+ .transition(DrawableTransitionOptions.withCrossFade())
+ .into(holder.getImg());
}
@Override
public int getItemCount() {
- return authors.length;
+ return this.posts.size();
}
}
diff --git a/app/src/main/java/fr/delcroix_dupont/MainActivity.java b/app/src/main/java/fr/delcroix_dupont/MainActivity.java
index a67b912..43c4cc2 100644
--- a/app/src/main/java/fr/delcroix_dupont/MainActivity.java
+++ b/app/src/main/java/fr/delcroix_dupont/MainActivity.java
@@ -80,6 +80,7 @@ public class MainActivity extends AppCompatActivity {
call.enqueue(new Callback<RedditPost>() {
@Override
public void onResponse(Call<RedditPost> call, Response<RedditPost> response) {
+ homeFragment.setRedditPostData(response.body().getData().getChildren());
}
@Override
diff --git a/app/src/main/java/fr/delcroix_dupont/MyViewHolder.java b/app/src/main/java/fr/delcroix_dupont/MyViewHolder.java
index a2d6d26..73708c6 100644
--- a/app/src/main/java/fr/delcroix_dupont/MyViewHolder.java
+++ b/app/src/main/java/fr/delcroix_dupont/MyViewHolder.java
@@ -2,6 +2,7 @@ package fr.delcroix_dupont;
import android.view.View;
import android.widget.Button;
+import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
@@ -12,6 +13,7 @@ import fr.delcroix_dupont.ui.home.author.FragmentAuthor;
public class MyViewHolder extends RecyclerView.ViewHolder {
private final Button authorButton;
private final TextView upvotes, downvotes;
+ private final ImageView img;
private final MainActivity main;
public MyViewHolder(MainActivity main, @NonNull View itemView) {
@@ -20,6 +22,7 @@ public class MyViewHolder extends RecyclerView.ViewHolder {
this.authorButton = (Button) itemView.findViewById(R.id.authorButton);
this.upvotes = (TextView) itemView.findViewById(R.id.nbUpvotes);
this.downvotes = (TextView) itemView.findViewById(R.id.nbDownvotes);
+ this.img = (ImageView) itemView.findViewById(R.id.imageView);
setupListener();
}
@@ -40,4 +43,6 @@ public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView getDownvotes(){
return this.downvotes;
}
+
+ public ImageView getImg() { return this.img; }
}
diff --git a/app/src/main/java/fr/delcroix_dupont/api/post/Data__1.java b/app/src/main/java/fr/delcroix_dupont/api/post/Data__1.java
index 05efe79..0061af2 100644
--- a/app/src/main/java/fr/delcroix_dupont/api/post/Data__1.java
+++ b/app/src/main/java/fr/delcroix_dupont/api/post/Data__1.java
@@ -14,6 +14,9 @@ public class Data__1 {
@SerializedName("downs")
@Expose
private Integer downs;
+ @SerializedName("thumbnail")
+ @Expose
+ private String thumbnail;
@SerializedName("name")
@Expose
private String name;
@@ -69,6 +72,14 @@ public class Data__1 {
this.downs = downs;
}
+ public String getThumbnail() {
+ return thumbnail;
+ }
+
+ public void setThumbnail(String thumbnail) {
+ this.thumbnail = thumbnail;
+ }
+
public String getName() {
return name;
}
diff --git a/app/src/main/java/fr/delcroix_dupont/api/user/Data.java b/app/src/main/java/fr/delcroix_dupont/api/user/Data.java
index 6212ed1..bc04753 100644
--- a/app/src/main/java/fr/delcroix_dupont/api/user/Data.java
+++ b/app/src/main/java/fr/delcroix_dupont/api/user/Data.java
@@ -1,5 +1,8 @@
package fr.delcroix_dupont.api.user;
+import com.google.gson.annotations.Expose;
+import com.google.gson.annotations.SerializedName;
+
import java.util.List;
public class Data {
diff --git a/app/src/main/java/fr/delcroix_dupont/api/user/RedditUser.java b/app/src/main/java/fr/delcroix_dupont/api/user/RedditUser.java
index 1a9c84d..6746218 100644
--- a/app/src/main/java/fr/delcroix_dupont/api/user/RedditUser.java
+++ b/app/src/main/java/fr/delcroix_dupont/api/user/RedditUser.java
@@ -1,5 +1,8 @@
package fr.delcroix_dupont.api.user;
+import com.google.gson.annotations.Expose;
+import com.google.gson.annotations.SerializedName;
+
public class RedditUser {
@SerializedName("date")
diff --git a/app/src/main/java/fr/delcroix_dupont/ui/home/HomeFragment.java b/app/src/main/java/fr/delcroix_dupont/ui/home/HomeFragment.java
index e0f011a..77f6ded 100644
--- a/app/src/main/java/fr/delcroix_dupont/ui/home/HomeFragment.java
+++ b/app/src/main/java/fr/delcroix_dupont/ui/home/HomeFragment.java
@@ -13,10 +13,14 @@ import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
+import java.util.List;
+
+import fr.delcroix_dupont.HomeAdapter;
import fr.delcroix_dupont.HomeGridAdapter;
import fr.delcroix_dupont.HomeListAdapter;
import fr.delcroix_dupont.MainActivity;
import fr.delcroix_dupont.R;
+import fr.delcroix_dupont.api.post.Child;
import fr.delcroix_dupont.databinding.FragmentHomeBinding;
public class HomeFragment extends Fragment {
@@ -24,7 +28,7 @@ public class HomeFragment extends Fragment {
private FragmentHomeBinding binding;
private RecyclerView recyclerView;
private RecyclerView.LayoutManager layoutManager;
- private HomeListAdapter customAdapter;
+ private HomeAdapter adapter;
private View root;
private ImageButton switchButton;
private MainActivity main;
@@ -33,10 +37,6 @@ public class HomeFragment extends Fragment {
private boolean isList;
- private String[] authors = new String[]{"MiraakTV", "BoBsmil3Y", "MiraakTélévision", "Bobmarl3y", "Moi"};
- private String[] upvotes = new String[]{"0", "0", "999", "999", "235"};
- private String[] downvotes = new String[]{"999", "999", "0", "0", "3534"};
-
public HomeFragment(MainActivity main) { this.main = main; }
public View onCreateView(@NonNull LayoutInflater inflater,
@@ -73,7 +73,8 @@ public class HomeFragment extends Fragment {
public void switchGridList(){
this.recyclerView.setLayoutManager(this.isList ? this.gridLayoutManager : this.linearLayoutManager);
- this.recyclerView.setAdapter(this.isList ? new HomeGridAdapter(this.main, this.authors) : new HomeListAdapter(this.main, this.authors, this.upvotes, this.downvotes));
+ this.adapter = this.isList ? new HomeGridAdapter(this.main, this.adapter.getPosts()) : new HomeListAdapter(this.main, this.adapter.getPosts());
+ this.recyclerView.setAdapter(this.adapter);
this.switchButton.setImageResource(this.isList ? R.drawable.list : R.drawable.menu);
this.isList = !this.isList;
}
@@ -87,8 +88,12 @@ public class HomeFragment extends Fragment {
layoutManager = new LinearLayoutManager(root.getContext());
recyclerView.setLayoutManager(layoutManager);
- customAdapter = new HomeListAdapter(main, authors, upvotes, downvotes);
- recyclerView.setAdapter(customAdapter);
+ adapter = new HomeListAdapter(main);
+ recyclerView.setAdapter(adapter);
+ }
+
+ public void setRedditPostData(List<Child> posts){
+ adapter.setRedditPostData(posts);
}
@Override
--
GitLab