1. Gradle
implementation 'com.android.support:recyclerview-v7:28.0.0'
저 뒤에 숫자는 자기꺼랑 버전을 맞춰준다.
2. Layout
RecyclerView랑 안에 들어갈 layout하나씩 만들어준다.
class MainRecyclerViewAdaptor: RecyclerView.Adapter<RecyclerView.ViewHolder>(){
//각각의 아이템의 디자인레이아웃을 불러온다
override fun onCreateViewHolder(p0: ViewGroup, p1: Int): RecyclerView.ViewHolder {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
//아이템의 개수를 카운트한다
override fun getItemCount(): Int {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
//각각의 아이템의 데이터를 바인딩 시켜준다
override fun onBindViewHolder(p0: RecyclerView.ViewHolder, p1: Int) {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
}
package com.example.lipnus.lipnus_example
import android.os.Bundle
import android.support.constraint.R.id.parent
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.GridLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.ViewGroup
import android.view.View
import android.widget.GridLayout
import android.widget.ImageView
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()
var gridManager = GridLayoutManager(this, 3)
lipnus_recyclerView.layoutManager = gridManager
gridManager.spanSizeLookup = object : GridLayoutManager.SpanSizeLookup(){
override fun getSpanSize(position: Int): Int {
if(position==0){
return 3 //3칸 차지
}
return 1 //1칸 차지
}
}
}
class MainRecyclerViewAdaptor: RecyclerView.Adapter<RecyclerView.ViewHolder>(){
var images = arrayOf(R.drawable.s1, R.drawable.s2, R.drawable.s3, R.drawable.s4)
//포지션 값에 따라서 타입을 바꿔준다
override fun getItemViewType(position: Int): Int {
if(position==0) return 0
return 1
//리턴값은 onCreateViewHolder의 viewType매개변수로 들어간다
}
//각각의 아이템의 디자인레이아웃을 불러온다
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
var view = LayoutInflater.from(parent!!.context).inflate(R.layout.item_layout, parent, false)
var params = view.layoutParams
if(viewType==0){
params.height = parent.measuredWidth
params.width = parent.measuredWidth
}else{
//인스타처럼 3칸짜리 정사각형 만들기
params.height = parent.measuredWidth/3
params.width = parent.measuredWidth/3
view.layoutParams = params
}
return CustomViewHolder(view)
}
class CustomViewHolder(view: View) : RecyclerView.ViewHolder(view) {
var imageview: ImageView? = null;
init {
imageview = view!!.findViewById(R.id.LayoutItem_imageview)
}
}
//아이템의 개수를 카운트한다
override fun getItemCount(): Int {
return images.size
}
//각각의 아이템에 데이터를 바인딩시켜준다
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
var view = holder as CustomViewHolder
view!!.imageview!!.setImageResource(images[position])
}
}
}
'Android > Kotlin' 카테고리의 다른 글
Class - 기본 (0) | 2018.11.20 |
---|---|
IntelliJ IDEA에서 Kotlin실행 (0) | 2018.11.20 |
Recyclerview(LinearLayout) (0) | 2018.11.19 |
[Android] DatePicker, TimePicker (0) | 2018.07.18 |
코틀린 기초 (0) | 2018.07.13 |