본문 바로가기

ANDROID

간단하게 RecyclerView 를 달아보자

해당예제는 Kotlin 으로 작성 하였으며 정말 간단한 내용만 추가하여 구성 하였다.


1. Data 클래스 추가  (Data.kt)

class Data (val title:String, val content:String, imgUrl:String) { }

 

2. item XML 생성 (recyclerview_item.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:padding="15dp"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/imgUrl"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:layout_marginRight="10dp"
        android:src="@mipmap/ic_launcher"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TextView
            android:id="@+id/titleTxt"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"/>

        <TextView
            android:id="@+id/contentTxt"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:textSize="18sp"
            android:textColor="#000000"/>

    </LinearLayout>
</LinearLayout>

 

3. Adapter 클래스 생성 (ListAdapter.kt)

package com.example.recylcerviewproject.adapters

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 androidx.recyclerview.widget.RecyclerView
import com.example.recylcerviewproject.R
import com.example.recylcerviewproject.datas.Data

class ListAdapter(val list : ArrayList<Data>, val context:Context) : RecyclerView.Adapter<ListAdapter.ViewHolder>()
{
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        return ListAdapter.ViewHolder(LayoutInflater.from(context)
            .inflate(R.layout.recylcerview_item,parent ,false))

    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.bind(list[position],context)
    }

    override fun getItemCount(): Int {
        return list.count()
    }


    class ViewHolder (itemView: View? ) : RecyclerView.ViewHolder(itemView!!){

        val imgUrl = itemView?.findViewById<ImageView>(R.id.imgUrl)
        val contentTxt = itemView?.findViewById<TextView>(R.id.contentTxt)
        val titleTxt  = itemView?.findViewById<TextView>(R.id.titleTxt)

        fun bind(itemData : Data? , context: Context){

            //imgUrl
            contentTxt?.text = itemData!!.content
            titleTxt?.text = itemData!!.title
        }

    }
}

 

4. 리스트 호출 및 데이터 추가 (MainActvity.kt) (xml 생략)

package com.example.recylcerviewproject

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.recylcerviewproject.adapters.ListAdapter
import com.example.recylcerviewproject.datas.Data
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity()
{

    val mList = ArrayList<Data>()
    lateinit var mListADapter : ListAdapter

    override fun onCreate(savedInstanceState: Bundle?)
    {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        mList.add(Data("제목1","타이틀1",""))
        mList.add(Data("제목2","타이틀2",""))
        mList.add(Data("제목3","타이틀3",""))
        mList.add(Data("제목4","타이틀3",""))
        mList.add(Data("제목5","타이틀3",""))

        mListADapter = ListAdapter(mList,this)
        recylcerView.adapter = mListADapter

        val lm = LinearLayoutManager(this)
        recylcerView.layoutManager = lm
        recylcerView.setHasFixedSize(true)


    }
}

 

5. 결과