CodeKata
스탠다드반 과제
Multi-View RecyclerView
data class
@Parcelize
data class Data(
val type:Int,
val tvName: String,
val num1:Int,
val num2:Int,
val num3: Int,
val num4: Int,
val YM: String,
val price: Double
):Parcelable
DataList
//DataMode
const val item_card1 = 1
const val item_card2 = 2
const val item_card3 = 3
fun dataList(): MutableList<Data> {
return mutableListOf(
Data(
type = item_card1,
tvName = "Anderson",
num1 = 2423,
num2 = 3581,
num3 = 9503,
num4 = 2412,
YM = "21/24",
price = 3100.30
),
Data(
type = item_card2,
tvName = "John",
num1 = 1234,
num2 = 5678,
num3 = 9876,
num4 = 5432,
YM = "21/24",
price = 2459.60
),
Data(
type = item_card3,
tvName = "Mash",
num1 = 4564,
num2 = 4564,
num3 = 7895,
num4 = 4125,
YM = "21/24",
price = 4567.20
),
)
}
Adapter
class DataAdapter(private val data: MutableList<Data>) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
interface ItemClick{
fun onClick(view:View, position: Int)
}
var itemClick:ItemClick? = null
override fun getItemViewType(position: Int): Int {
return data[position].type
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
val view: View?
return when (viewType) { //todo sealed class -> else 제거해보기
item_card1 -> {
val binding = ItemCard1Binding.inflate(LayoutInflater.from(parent.context), parent, false)
Card1ViewHolder(binding)
}
item_card2 -> {
val binding = ItemCard2Binding.inflate(LayoutInflater.from(parent.context), parent, false)
Card2ViewHolder(binding)
}
else -> { //item_card3
val binding = ItemCard3Binding.inflate(LayoutInflater.from(parent.context), parent, false)
Card3ViewHolder(binding)
}
}
}
override fun getItemCount(): Int {
return data.size
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
when (data[position].type) {
item_card1 -> {
(holder as Card1ViewHolder).bind(data[position])
holder.setIsRecyclable(false)
holder.itemView.setOnClickListener {
itemClick?.onClick(it,position)
}
}
item_card2 -> {
(holder as Card2ViewHolder).bind(data[position])
holder.setIsRecyclable(false)
holder.itemView.setOnClickListener {
itemClick?.onClick(it,position)
}
}
else -> { //item_card3
(holder as Card3ViewHolder).bind(data[position])
holder.setIsRecyclable(false)
holder.itemView.setOnClickListener {
itemClick?.onClick(it,position)
}
}
}
}
}
MainActivity
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
binding = ActivityMainBinding.inflate(layoutInflater)
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContentView(binding.root)
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
insets
}
showTotalPrice()
recyclerViewDivider()
initRecyclerView()
}
//item 간격 추가
private fun recyclerViewDivider() {
binding.recyclerView.addItemDecoration(object : ItemDecoration() {
override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
super.getItemOffsets(outRect, view, parent, state)
outRect.bottom = 40
}
})
}
private fun initRecyclerView() {
val adapter = DataAdapter(dataList())
val dataList = dataList()
binding.recyclerView.adapter = adapter
binding.recyclerView.layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
adapter.itemClick = object : DataAdapter.ItemClick {
override fun onClick(view: View, position: Int) {
val selectedData = dataList[position]
val intent = Intent(this@MainActivity, DetailActivity::class.java)
intent.putExtra("selectedData", selectedData)
startActivity(intent)
}
}
}
private fun showTotalPrice() {
val totalPrice = dataList().sumOf { it.price }
val decimal = DecimalFormat("#,##,###.00")
binding.tvDollar.text = decimal.format(totalPrice)
}
}
xml
이렇게 똑같은 총 3개의 itemView 생성
더보기
<?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:tool="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginTop="20dp"
android:background="@drawable/card_radius"
android:backgroundTint="@color/myBlue">
<TextView
android:id="@+id/tvName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:textColor="@color/white"
android:textSize="18sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tool:text="Anderson" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="20dp"
android:text="A Debit Card"
android:textColor="@color/white"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="@id/tvName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@id/tvName" />
<TextView
android:id="@+id/tvCardNum1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="15dp"
android:layout_marginTop="5dp"
android:textColor="@color/white"
android:textSize="23sp"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tvName"
tool:text="2423" />
<TextView
android:id="@+id/tvCardNum2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="15dp"
android:layout_marginTop="5dp"
android:textColor="@color/white"
android:textSize="23sp"
android:textStyle="bold"
app:layout_constraintStart_toEndOf="@id/tvCardNum1"
app:layout_constraintTop_toBottomOf="@id/tvName"
tool:text="2423" />
<TextView
android:id="@+id/tvCardNum3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="15dp"
android:layout_marginTop="5dp"
android:textColor="@color/white"
android:textSize="23sp"
android:textStyle="bold"
app:layout_constraintStart_toEndOf="@id/tvCardNum2"
app:layout_constraintTop_toBottomOf="@id/tvName"
tool:text="2423" />
<TextView
android:id="@+id/tvCardNum4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="15dp"
android:layout_marginTop="5dp"
android:textColor="@color/white"
android:textSize="23sp"
android:textStyle="bold"
app:layout_constraintStart_toEndOf="@id/tvCardNum3"
app:layout_constraintTop_toBottomOf="@id/tvName"
tool:text="2423" />
<TextView
android:id="@+id/tvYM"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="15dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="13dp"
android:textColor="@color/white"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="@id/tvCardNum1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@id/tvCardNum1"
tool:text="21 / 24" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Your Balance"
android:textColor="@color/white"
app:layout_constraintStart_toStartOf="@id/tvCardNum1"
app:layout_constraintTop_toBottomOf="@id/tvCardNum1" />
<TextView
android:id="@+id/won"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:text="$"
android:textColor="@color/white"
android:textSize="30sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="@id/tvCardNum1" />
<TextView
android:id="@+id/tvPrice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:textColor="@color/white"
android:textSize="30sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="@id/won"
app:layout_constraintStart_toEndOf="@id/won"
tool:text="3,100.30" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Visa"
android:textColor="@color/white"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="@id/won"
app:layout_constraintEnd_toEndOf="@id/tvYM" />
</androidx.constraintlayout.widget.ConstraintLayout>
문제에 해당해서만 풀었던 것 같다.
서로 데이터 형식이 다를 수도 있는 점을 못한 것 같아서 다시 만들고 있다.
'Kotlin > TIL' 카테고리의 다른 글
TIL (04.17) (0) | 2024.04.17 |
---|---|
TIL (04.16) (0) | 2024.04.16 |
TIL (04.12) (0) | 2024.04.12 |
TIL (04.09) (0) | 2024.04.09 |
TIL (04.08) (0) | 2024.04.08 |