ajout de l'affichage du fetch de l'API dans une RecyclerView

pull/10/head
Alexis1663 2 years ago
parent 66117fc62e
commit 173f6d8c9c

@ -0,0 +1,34 @@
package adaptator
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 api.User
import but.androidstudio.tetris.R
import javax.xml.xpath.XPath
class RecyclerViewAdaptator(var userData: List<User>): RecyclerView.Adapter<RecyclerViewAdaptator.ItemViewHolder>(){
class ItemViewHolder(private val view: View) : RecyclerView.ViewHolder(view) {
val textViewUsername: TextView = view.findViewById(R.id.item_username)
val textViewXp: TextView = view.findViewById(R.id.item_xp)
val textViewCountry: TextView = view.findViewById(R.id.item_country)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemViewHolder {
val adapterLayout = LayoutInflater.from(parent.context)
.inflate(R.layout.item_layout, parent, false)
return ItemViewHolder(adapterLayout)
}
override fun getItemCount(): Int = userData.size
override fun onBindViewHolder(holder: ItemViewHolder, position: Int) {
holder.textViewUsername.text = userData[position].username
holder.textViewXp.text = userData[position].xp.toString()
holder.textViewCountry.text = userData[position].country
}
}

@ -1,22 +1,16 @@
package but.androidstudio.tetris
import adaptator.RecyclerViewAdaptator
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import api.TetrisAPI
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import api.TetrisClient
import api.User
import com.google.gson.Gson
import com.google.gson.GsonBuilder
import okhttp3.OkHttpClient
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.io.InputStream
import java.security.KeyStore
import java.security.SecureRandom
@ -26,7 +20,9 @@ import javax.net.ssl.TrustManagerFactory
import javax.net.ssl.X509TrustManager
class ClassementFragment : Fragment() {
class InfoUserFragment : Fragment() {
private lateinit var recyclerView: RecyclerView
private lateinit var adaptator: RecyclerViewAdaptator
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
@ -36,7 +32,13 @@ class ClassementFragment : Fragment() {
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_classement, container, false)
val view = inflater.inflate(R.layout.fragment_info_user, container, false)
// Set up RecyclerView
recyclerView = view.findViewById<RecyclerView>(R.id.tableUser)
recyclerView.layoutManager = LinearLayoutManager(requireContext())
adaptator =RecyclerViewAdaptator(listOf())
recyclerView.adapter = adaptator
return view
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
@ -73,9 +75,8 @@ class ClassementFragment : Fragment() {
println("Error: ${error.message}")
} else {
// Handle success
users?.forEach {
println("User: ${it.username}")
}
adaptator.userData = users!!
recyclerView.adapter?.notifyDataSetChanged()
}
}
}

@ -39,7 +39,7 @@ class MainFragment : Fragment() {
buttonClassement.setOnClickListener {
val fragmentManager = requireActivity().supportFragmentManager
fragmentManager.beginTransaction()
.replace(R.id.homeLayout, ClassementFragment())
.replace(R.id.homeLayout, InfoUserFragment())
.commit()
}
}

@ -1,15 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ClassementFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/hello_blank_fragment" />
</FrameLayout>

@ -0,0 +1,38 @@
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Username"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="XP"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Country"
android:textStyle="bold" />
</LinearLayout>
<androidx.recyclerview.widget.RecyclerView
android:orientation="vertical"
android:id="@+id/tableUser"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>

@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/item_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<TextView
android:id="@+id/item_xp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<TextView
android:id="@+id/item_country"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</LinearLayout>
Loading…
Cancel
Save