티스토리 뷰
build.gradle (Module: app)
dependencies {
// RecyclerView 생성 (혀가 길지 않은 개발자)
implementation 'androidx.recyclerview:recyclerview:1.1.0'
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
recyclerview_item.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/txtView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="26dp"
android:text="혀가 길지 않은 개발자"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
RecyclerViewAdapter.kt
package com.jwsoft.kotlinproject
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
class RecyclerViewAdapter(private val arrData: ArrayList<String>)
: RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder>() {
var data: ArrayList<String> = arrData
// inner 클래스는 outer 클래스의 변수를 사용 할 수 있다. (data: ArrayList<String>)
inner class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
var txtView: TextView = itemView.findViewById(R.id.txtView)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
val context = parent.context
val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
val view = inflater.inflate(R.layout.recyclerview_item, parent, false)
return MyViewHolder(view)
}
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
holder.txtView.text = data[position] + "번째 혀가 길지 않은 개발자"
}
override fun getItemCount(): Int {
return data.size
}
}
MainActivity.kt
package com.jwsoft.kotlinproject
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
var arrData = ArrayList<String>()
for (i in 1..100) {
arrData.add(i.toString())
}
val recyclerView: RecyclerView = findViewById(R.id.recyclerView)
recyclerView.layoutManager = LinearLayoutManager(
applicationContext,
RecyclerView.VERTICAL,
false)
val adapter = RecyclerViewAdapter(arrData)
recyclerView.adapter = adapter
recyclerView.addItemDecoration(DividerItemDecoration(this, LinearLayout.VERTICAL))
}
}
'Android > Kotlin' 카테고리의 다른 글
[Kotlin] Handler.post (0) | 2020.06.27 |
---|---|
[Kotlin] Handler (0) | 2020.06.27 |
[Kotlin] 4. Button 활용 (0) | 2020.06.11 |
[Kotlin] 3. TextView 접근 (0) | 2020.06.11 |
[Kotlin] 변수 선언 및 할당 (0) | 2020.06.11 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 안드로이드 #코틀린 #Android #Kotlin
- View
- Kotlin
- TabLayout
- Design Pattern
- Android
- fragment
- XML
- 안드로이드
- ViewPager2
- Vue.js #Vue.js + javascript
- Livedata
- Architecture Pattern
- ViewModel
- 코틀린
- Intent
- JSONArray
- CoordinatorLayout
- activity
- recyclerview
- java
- James Kim
- handler
- 혀가 길지 않은 개발자
- JSONObject
- MVVM
- coroutine
- ArrayList
- 자바
- DataBinding
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
글 보관함