Android/Kotlin

Recyclerview(LinearLayout)

lipnus 2018. 11. 19. 22:03
반응형


package com.example.lipnus.list

import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import kotlinx.android.synthetic.main.activity_main.*


class MainActivity : AppCompatActivity() {

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

lipnus_recyclerview.adapter = MainRecyclerViewAdaptor()
lipnus_recyclerview.layoutManager = LinearLayoutManager(this)
}

class MainRecyclerViewAdaptor: RecyclerView.Adapter<RecyclerView.ViewHolder>(){

var images = arrayOf(R.drawable.s1, R.drawable.s2, R.drawable.s3, R.drawable.s4)
var titles = arrayOf("민경", "민정", "만두", "딤섬")
var countries = arrayOf("영국", "프랑스", "체코", "미국")

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
var view = LayoutInflater.from(parent!!.context).inflate(R.layout.item_layout, parent, false)

return CustomViewHoler(view)
}


class CustomViewHoler(view: View) : RecyclerView.ViewHolder(view) {
var imageview: ImageView? = null
var textview: TextView? = null
var textview2: TextView? = null

init {
imageview = view!!.findViewById(R.id.profile_imageview)
textview = view!!.findViewById(R.id.title_textview)
textview2 = view!!.findViewById(R.id.country_textview)
}
}


override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
var view = holder as CustomViewHoler
view.imageview!!.setImageResource(images[position])
view.textview!!.setText(titles[position])
view.textview2!!.setText(countries[position])
}


override fun getItemCount(): Int {
return images.size
}
}
}






따로 클래스로 만든 형태 (Item은 String리스트로만 처리)

class SettingRvAdapter: RecyclerView.Adapter<RecyclerView.ViewHolder>() {


val items = ArrayList<String>()

//클릭 인터페이스 정의
interface ItemClickListener {
fun onClickUnPurchased(msg: Int)
}

//클릭리스너
private lateinit var itemClickListner: ItemClickListener


//클릭리스너 등록
fun setItemClickListener(itemClickListener: ItemClickListener) {
this.itemClickListner = itemClickListener
}



//각각의 아이템의 디자인레이아웃을 불러온다
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
var view = LayoutInflater.from(parent!!.context).inflate(R.layout.item_setting, parent, false)
return CustomViewHolder(view)
}


//커스텀 뷰홀더
class CustomViewHolder(view: View): RecyclerView.ViewHolder(view){
var titleTv: TextView = view.findViewById(R.id.item_setting_title_tv)
}


//아이템을 추가한다
fun addItem(item: String){
items.add(item)
}


//아이템의 개수를 반환
override fun getItemCount(): Int {
return items.size
}


//각각의 아이템의 데이터를 바인딩 시켜준다
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, pos: Int) {
var view = holder as CustomViewHolder
view.titleTv.text = items[pos]
}

}


반응형

'Android > Kotlin' 카테고리의 다른 글

Class - 기본  (0) 2018.11.20
IntelliJ IDEA에서 Kotlin실행  (0) 2018.11.20
RecyclerView(GridLayout)  (0) 2018.11.19
[Android] DatePicker, TimePicker  (0) 2018.07.18
코틀린 기초  (0) 2018.07.13