티스토리 뷰
build.gradle (Module: app)
dependencies {
implementation 'androidx.recyclerview:recyclerview:1.1.0'
implementation 'androidx.cardview:cardview:1.0.0'
}
cardview_item.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
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="120dp"
android:layout_margin="20dp"
android:padding="10dp"
app:cardElevation="10dp"
app:cardCornerRadius="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="@+id/ivProfile"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="center"
android:layout_marginLeft="10dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_marginLeft="10dp">
<TextView
android:id="@+id/tvName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="James Kim"
android:textSize="30dp"
android:textStyle="bold"
android:gravity="center"/>
<TextView
android:id="@+id/tvPhone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="010-1234-5678"
android:textSize="16dp"
android:textStyle="italic"
android:gravity="center"/>
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recylcerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"/>
<Button
android:id="@+id/btnAdd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAllCaps="false"
android:text="Add Person"
android:textSize="30dp" />
</LinearLayout>
ItemCard.kt
package com.jwsoft.kotlinproject
data class ItemCard(var imageRes: Int,
var backgroundRes: Int,
var name: String,
var phone: String)
MyRecyclerView.kt
package com.jwsoft.kotlinproject
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
class MyRecyclerView(items: ArrayList<ItemCard>) : RecyclerView.Adapter<MyRecyclerView.MyViewHolder>() {
var items = items
inner class MyViewHolder(itemView: View): RecyclerView.ViewHolder(itemView) {
var ivProfile: ImageView = itemView.findViewById(R.id.ivProfile)
var tvName: TextView = itemView.findViewById(R.id.tvName)
var tvPhone: TextView = itemView.findViewById(R.id.tvPhone)
fun bind(position: Int) {
ivProfile.setImageResource(items[position].imageRes)
ivProfile.setBackgroundResource(items[position].backgroundRes)
tvName.text = items[position].name
tvPhone.text = items[position].phone
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
val context = parent.context
val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
val view: View = inflater.inflate(R.layout.cardview_item, parent, false)
return MyViewHolder(view)
}
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
holder.bind(position)
}
override fun getItemCount(): Int = items.size
fun addPerson(item: ItemCard) {
items.add(item)
}
}
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
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
var items: ArrayList<ItemCard> = ArrayList()
for (i in 0 until 10) {
items.add(ItemCard(
R.drawable.ic_launcher_foreground,
R.drawable.ic_launcher_background,
"James Kim $i",
"010-1234-5678")
)
}
var adapter = MyRecyclerView(items)
recylcerView.adapter = adapter
recylcerView.layoutManager = LinearLayoutManager(
this,
RecyclerView.VERTICAL,
false
)
btnAdd.setOnClickListener {
adapter.addPerson(ItemCard(
R.drawable.ic_launcher_foreground,
R.drawable.ic_launcher_background,
"Jason Park",
"010-1111-1111")
)
adapter.notifyDataSetChanged()
}
}
}
'Android > Kotlin' 카테고리의 다른 글
[Kotlin] SQLite + Singleton Pattern (0) | 2020.07.18 |
---|---|
[Kotlin] Intent (0) | 2020.07.16 |
[Kotlin] SQLite (0) | 2020.07.11 |
[Kotlin] ViewModel (0) | 2020.07.10 |
[Kotlin] DataBinding + LiveData + BindingAdapter (0) | 2020.07.09 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- java
- 혀가 길지 않은 개발자
- James Kim
- handler
- 안드로이드
- DataBinding
- CoordinatorLayout
- fragment
- Intent
- View
- TabLayout
- recyclerview
- 코틀린
- Architecture Pattern
- ViewModel
- Kotlin
- ViewPager2
- 자바
- ArrayList
- Livedata
- 안드로이드 #코틀린 #Android #Kotlin
- JSONObject
- Vue.js #Vue.js + javascript
- Design Pattern
- coroutine
- activity
- XML
- MVVM
- Android
- JSONArray
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함