2023.7.29
1. μ€λΉ
- μ΄μ κ²μλ¬Ό(https://hibread.tistory.com/75)μ μ°μ₯μ μΌλ‘ μ± μ 보λ₯Ό λ°μ νλ©΄μ μΆλ ₯ν λ μ¬μ©ν 리μ¬μ΄νλ¬λ·°λ₯Ό ν΄λ¦νμ λ μ± μ λ³΄κ° λνλλ μ‘ν°λΉν°λ₯Ό λ§λ€ κ²μ΄λ€.
- μ΄μ μ μ¬μ©ν νμΌ: BookAdapter.kt
- activity_second.xml, SecondActivity.kt
2. xml μμ±
- activity_second.xml
<?xml version="1.0" encoding="utf-8"?>
<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"
android:orientation="vertical"
tools:context=".SecondActivity">
<ImageView
android:id="@+id/imgView"
android:layout_width="120dp"
android:layout_height="120dp"
android:background="#000000"/>
<TextView
android:id="@+id/textTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="μ±
μ λͺ©"/>
<TextView
android:id="@+id/textAuthor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="μ μ"/>
<TextView
android:id="@+id/textPulisher"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="μΆνμ¬"/>
<TextView
android:id="@+id/textPubDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="μΆκ°μΌ"/>
<TextView
android:id="@+id/textStory"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="μ€κ±°λ¦¬"/>
</LinearLayout>
3. BookAdapter.kt μ½λ μΆκ°
- 리μ¬μ΄νλ¬λ·°μ μμ΄ν μ ν΄λ¦νμ λ κ·Έ μμ΄ν μ μ 보μ ν¨κ» μ‘ν°λΉν° μ΄λ
import android.content.Intent
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
class BookAdapter(private var bookList: List<NaverBookItem>) : RecyclerView.Adapter<BookAdapter.BookViewHolder>() {
...
override fun onBindViewHolder(holder: BookViewHolder, position: Int) { // μ€μ νλ©΄μ λ°μ΄ν°μ λ μ΄μμμ μ°κ²°
val book = bookList[position]
holder.bind(book)
// μμ΄ν
ν΄λ¦ μ΄λ²€νΈ
holder.itemView.setOnClickListener {
val intent = Intent(holder.itemView.context, SecondActivity::class.java)
intent.putExtra("bookTitle", bookList[position].title)
intent.putExtra("bookAuthor", bookList[position].author)
intent.putExtra("bookPublisher", bookList[position].publisher)
intent.putExtra("bookPubDate", bookList[position].pubdate)
intent.putExtra("bookImgUrl", bookList[position].image)
intent.putExtra("bookStory", bookList[position].description)
ContextCompat.startActivity(holder.itemView.context, intent, null)
}
}
...
}
4. SecondActivity.kt μμ±
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.ImageView
import android.widget.TextView
import com.bumptech.glide.Glide
class SecondActivity : AppCompatActivity() {
lateinit var imgView: ImageView
lateinit var textTitle: TextView
lateinit var textAuthor: TextView
lateinit var textPublisher: TextView
lateinit var textPubDate: TextView
lateinit var textStory: TextView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_second)
imgView = findViewById(R.id.imgView)
textTitle = findViewById(R.id.textTitle)
textAuthor = findViewById(R.id.textAuthor)
textPublisher = findViewById(R.id.textPulisher)
textPubDate = findViewById(R.id.textPubDate)
textStory = findViewById(R.id.textStory)
val title = intent.getStringExtra("bookTitle")
val author = intent.getStringExtra("bookAuthor")
val publisher = intent.getStringExtra("bookPubliser")
val pubDate = intent.getStringExtra("bookPubDate")
val imgUrl = intent.getStringExtra("bookImgUrl")
val story = intent.getStringExtra("bookStory")
Glide.with(this).load(imgUrl).into(imgView)
textTitle.text = title
textAuthor.text = author
textPublisher.text = publisher
textPubDate.text = pubDate
textStory.text = story
}
}
κ²°κ³Ό