API: https://reqres.in/api/users?page=2
Method: GET
Resposnse:
{
“page”: 2, “per_page”: 6, “total”: 12, “total_pages”: 2,
“data”: [
{ “id”: 7, “email”: “michael.lawson@reqres.in”, “first_name”: “Michael”, “last_name”: “Lawson”,
“avatar”: “https://reqres.in/img/faces/7-image.jpg” },],
“support”: { “url”: “https://reqres.in/#support-heading”, “text”: “To keep ReqRes free, contributions towards server costs are appreciated!” } }
activity_home_page
<?xml version="1.0" encoding="utf-8"?>
<layout>
<LinearLayout 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=".HomePage"
android:orientation="vertical">
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/rec"/>
</LinearLayout>
</layout>
HomePage.kt
package com.androindian.sampleproject
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.LinearLayoutManager
import com.androindian.sampleproject.databinding.ActivityHomePageBinding
import retrofit2.Call
import retrofit2.Response
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
class HomePage : AppCompatActivity() {
var binding:ActivityHomePageBinding?=null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding=DataBindingUtil.setContentView(this,R.layout.activity_home_page)
var retrofit = Retrofit.Builder().baseUrl("https://reqres.in/api/")
.addConverterFactory(GsonConverterFactory.create()).build()
var retroInterface=retrofit.create(SampleProjectInteface::class.java)
var listcall: Call<ListRes>
listcall=retroInterface.loadData()
listcall.enqueue(object : retrofit2.Callback<ListRes?> {
override fun onResponse(call: Call<ListRes?>, response: Response<ListRes?>) {
var linearLayoutManager= LinearLayoutManager(this@HomePage,
LinearLayoutManager.VERTICAL, false)
binding?.rec?.layoutManager=linearLayoutManager
var customAdapter =CustomAdapter(this@HomePage,
response.body()?.data as List<ListInsideData>)
binding?.rec?.adapter = customAdapter
}
override fun onFailure(call: Call<ListRes?>, t: Throwable) {
TODO("Not yet implemented")
}
})
}
}
SampleProjectInteface.kt
package com.androindian.sampleproject
import okhttp3.RequestBody
import retrofit2.http.GET
import retrofit2.http.Headers
import retrofit2.http.Multipart
import retrofit2.http.POST
import retrofit2.http.Part
import retrofit2.http.Url
public interface SampleProjectInteface {
@Headers("Content-Type:application/json")
@GET("users?page=2")
fun loadData(): retrofit2.Call<ListRes>
}
CustomAdapter.kt
package com.androindian.sampleproject
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import androidx.recyclerview.widget.RecyclerView
import com.squareup.picasso.Picasso
class CustomAdapter(homePage: HomePage, data: List<ListInsideData>) : RecyclerView.Adapter<CustomAdapter.MyViewHolder>() {
var context: Context
var adpData:List<ListInsideData>
init {
context=homePage
adpData= data as List<ListInsideData>
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CustomAdapter.MyViewHolder {
var view=LayoutInflater.from(parent.context).inflate(R.layout.custom,parent,false)
return MyViewHolder(view)
}
override fun onBindViewHolder(holder: CustomAdapter.MyViewHolder, position: Int) {
holder.id?.text= adpData.get(holder.adapterPosition).id.toString()
holder.mail?.text= adpData.get(holder.adapterPosition).email
holder.fname?.text= adpData.get(holder.adapterPosition).firstName
holder.lname?.text= adpData.get(holder.adapterPosition).lastName
//holder.iv?.setImageResource(adpData[holder.adapterPosition].avatar)
Picasso.with(context).load(adpData.get(holder.adapterPosition).avatar).into(holder.iv)
holder.iv?.setOnClickListener {
Toast.makeText(context,""+holder.adapterPosition,Toast.LENGTH_LONG).show()
}
}
override fun getItemCount(): Int {
return adpData.size
}
inner class MyViewHolder(itemView: View):RecyclerView.ViewHolder(itemView) {
var id: TextView?=null
var mail: TextView?=null
var fname: TextView?=null
var lname: TextView?=null
var iv: ImageView?=null
init {
id=itemView.findViewById(R.id.id1)
mail=itemView.findViewById(R.id.email)
fname=itemView.findViewById(R.id.firtsname)
lname=itemView.findViewById(R.id.lastname)
iv=itemView.findViewById(R.id.iv)
}
}
}
private fun ImageView?.setImageResource(avatar: String?) {
}
custom.xml
<?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"
android:layout_margin="5dp"
android:background="@drawable/border">
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:id="@+id/iv"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_margin="5dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/id1"
android:textColor="#FF5722"
android:textSize="20sp"
android:textAllCaps="true"
android:textStyle="italic"
android:layout_margin="10dp"
android:padding="5dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/email"
android:textColor="#098"
android:textSize="12sp"
android:textAllCaps="true"
android:textStyle="italic"
android:layout_margin="5dp"
android:padding="5dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/firtsname"
android:textColor="#3F51B5"
android:textSize="12sp"
android:textAllCaps="true"
android:textStyle="italic"
android:layout_margin="5dp"
android:padding="5dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/lastname"
android:textColor="#E91E63"
android:textSize="12sp"
android:textAllCaps="true"
android:textStyle="italic"
android:layout_margin="5dp"
android:padding="5dp"/>
</LinearLayout>
</LinearLayout>
ListRes.kt
package com.androindian.sampleproject
import com.google.gson.annotations.Expose
import com.google.gson.annotations.SerializedName
class ListRes {
@SerializedName("page")
@Expose
var page: Int? = null
@SerializedName("per_page")
@Expose
var perPage: Int? = null
@SerializedName("total")
@Expose
var total: Int? = null
@SerializedName("total_pages")
@Expose
var totalPages: Int? = null
@SerializedName("data")
@Expose
var data: List<ListInsideData>? = null
@SerializedName("support")
@Expose
var support: Support? = null
}
Support.kt
package com.androindian.sampleproject
import com.google.gson.annotations.Expose
import com.google.gson.annotations.SerializedName
class Support {
@SerializedName("url")
@Expose
var url: String? = null
@SerializedName("text")
@Expose
var text: String? = null
}
ListInsideData.kt
package com.androindian.sampleproject
import com.google.gson.annotations.Expose
import com.google.gson.annotations.SerializedName
class ListInsideData {
@SerializedName("id")
@Expose
var id: Int? = null
@SerializedName("email")
@Expose
var email: String? = null
@SerializedName("first_name")
@Expose
var firstName: String? = null
@SerializedName("last_name")
@Expose
var lastName: String? = null
@SerializedName("avatar")
@Expose
var avatar: String? = null
}
build.gradle
plugins {
id("com.android.application")
id("org.jetbrains.kotlin.android")
}
android {
namespace = "com.androindian.sampleproject"
compileSdk = 34
defaultConfig {
applicationId = "com.androindian.sampleproject"
minSdk = 24
targetSdk = 33
versionCode = 1
versionName = "1.0"
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
isMinifyEnabled = false
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"
)
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = "1.8"
}
dataBinding{
enable=true
}
}
dependencies {
implementation("androidx.core:core-ktx:1.9.0")
implementation ("com.squareup.retrofit2:retrofit:2.4.0")
implementation ("com.google.code.gson:gson:2.8.9")
implementation ("com.squareup.retrofit2:converter-gson:2.4.0")
implementation("androidx.appcompat:appcompat:1.6.1")
implementation("com.google.android.material:material:1.10.0")
implementation("androidx.constraintlayout:constraintlayout:2.1.4")
testImplementation("junit:junit:4.13.2")
androidTestImplementation("androidx.test.ext:junit:1.1.5")
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
implementation ("com.squareup.picasso:picasso:2.5.2")
}