Skip to content

Commit

Permalink
onClick
Browse files Browse the repository at this point in the history
  • Loading branch information
salvator627 committed Sep 10, 2023
1 parent 4a12908 commit ead4416
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 4 deletions.
6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.INTERNET">
</uses-permission>

<uses-permission android:name="android.permission.INTERNET"></uses-permission>

<application
android:allowBackup="true"
Expand All @@ -13,6 +13,9 @@
android:supportsRtl="true"
android:theme="@style/Theme.Games"
tools:targetApi="31">
<activity
android:name=".DetailActivity"
android:exported="false" />
<activity
android:name=".MainActivity"
android:exported="true">
Expand Down
15 changes: 13 additions & 2 deletions app/src/main/java/com/example/games/GameAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@ import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions


class GameAdapter(private val list : ArrayList<PostResponse>): RecyclerView.Adapter<GameAdapter.ViewHolder>(){
class GameAdapter(private val list : ArrayList<PostResponse>):
RecyclerView.Adapter<GameAdapter.ViewHolder>(){

private var onItemclickcallback : onItemclick? = null
fun setOnItemclickcallback(onItemclick: onItemclick){
this.onItemclickcallback = onItemclick
}
inner class ViewHolder(itemview:View):RecyclerView.ViewHolder(itemview){
fun bind(postResponse: PostResponse){
with(itemView){
Expand All @@ -24,7 +30,9 @@ class GameAdapter(private val list : ArrayList<PostResponse>): RecyclerView.Adap
text.text = "${postResponse.title}"
dev.text = "${postResponse.developer}"


itemView.setOnClickListener {
onItemclickcallback?.onItemclicked(postResponse)
}
}
}
}
Expand All @@ -40,4 +48,7 @@ class GameAdapter(private val list : ArrayList<PostResponse>): RecyclerView.Adap
holder.bind(list[position])

}
interface onItemclick{
fun onItemclicked(data:PostResponse)
}
}
12 changes: 12 additions & 0 deletions app/src/main/java/com/example/games/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package com.example.games

import android.content.Intent
import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.games.detail.DetailActivity
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
Expand All @@ -14,6 +17,7 @@ class MainActivity : AppCompatActivity() {

private val list = ArrayList<PostResponse>()


override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
Expand All @@ -23,6 +27,7 @@ class MainActivity : AppCompatActivity() {
rv.layoutManager = LinearLayoutManager(this)
rv.setBackgroundColor(Color.DKGRAY)


RetrofitClient.instance.games().enqueue(object:Callback<ArrayList<PostResponse>>{
override fun onResponse(
call: Call<ArrayList<PostResponse>>,
Expand All @@ -31,6 +36,13 @@ class MainActivity : AppCompatActivity() {
response.body()?.let { list.addAll(it)}
val adapter = GameAdapter(list)
rv.adapter = adapter

adapter.setOnItemclickcallback(object : GameAdapter.onItemclick{
override fun onItemclicked(data: PostResponse) {
Toast.makeText(this@MainActivity, "hello ${data.title}", Toast.LENGTH_SHORT).show()
}

})
}

override fun onFailure(call: Call<ArrayList<PostResponse>>, t: Throwable) {
Expand Down
17 changes: 17 additions & 0 deletions app/src/main/java/com/example/games/detail/DetailActivity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.example.games.detail

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.example.games.R

class DetailActivity : AppCompatActivity() {

companion object{
const val EXTRA_DATA = "extra_data"
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_detail)
}
}
20 changes: 20 additions & 0 deletions app/src/main/res/layout/activity_detail.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?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=".detail.DetailActivity">

<ImageView
android:id="@+id/imageView3"
android:layout_width="413dp"
android:layout_height="179dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.608"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.003"
tools:srcCompat="@tools:sample/avatars" />
</androidx.constraintlayout.widget.ConstraintLayout>
10 changes: 10 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
repositories {
google()
mavenCentral()
}
dependencies {
classpath 'com.google.gms:google-services:4.3.13'
}
}
plugins {
id 'com.android.application' version '7.4.0' apply false
id 'com.android.library' version '7.4.0' apply false
Expand Down

0 comments on commit ead4416

Please sign in to comment.