λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°

Study/Android

[μ•ˆλ“œλ‘œμ΄λ“œ μ½”ν‹€λ¦°] ν”„λ‘œμ νŠΈ - RecyclerView(λ¦¬μ‚¬μ΄ν˜λŸ¬λ·°)λ₯Ό μ΄μš©ν•œ μ•‘ν‹°λΉ„ν‹° μ „ν™˜

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
    }
}

 

 

κ²°κ³Ό