//MainActivity.kt
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding //viewBinder
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
//viewBinder setting
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
//recyclerview setting
setupRecyclerview()
}
private fun setupRecyclerview(){
binding.contentMain.mainRecyclerView.apply {
layoutManager = LinearLayoutManager(context)
adapter = CustomAdapter(createDayToDoList()){dayToDo, position ->
Toast.makeText(
this@MainActivity,
"item${dayToDo.subCategory} 클릭",
Toast.LENGTH_SHORT
).show()
}
}
}
private fun createDayToDoList() : ArrayList<DayToDo>{ //test Data
return arrayListOf<DayToDo>(
DayToDo(
"대단원1",
"소단원1",
false
),DayToDo(
"대단원1",
"소단원2",
true
),DayToDo(
"대단원2",
"소단원3",
false
),DayToDo(
"대단원3",
"소단원4",
false
),DayToDo(
"대단원3",
"소단원4",
false
),DayToDo(
"대단원3",
"소단원4",
false
),DayToDo(
"대단원3",
"소단원4",
false
)
)
}
}
//CustomAdapter.kt
class DayToDo (var mainCategory: String, var subCategory: String, var isDone:Boolean)
class CustomAdapter (//main(일별) recyclerivew
private val dayToDoList: ArrayList<DayToDo>,
private val listener: (DayToDo,Int)->Unit //클릭이벤트 처리
) :
RecyclerView.Adapter<CustomAdapter.ViewHolder>(){
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { //recyclerview 생성시 호출
val v = MainListItemBinding.inflate(LayoutInflater.from(parent.context),parent,false)
return ViewHolder(v)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) { //특정 position의 데이터를 bind하기 위해 호출
holder.bindItem(dayToDoList[position])
holder.itemView.setOnClickListener{
listener(dayToDoList[position],position)
}
}
override fun getItemCount(): Int { //recyclerview 아이탬 개수를 얻기위해 호출
return dayToDoList.size
}
class ViewHolder(var listItemBinding: MainListItemBinding) : //recyclerview의 한 아이탬을 대변함
RecyclerView.ViewHolder(listItemBinding.root){
fun bindItem(todo : DayToDo){
listItemBinding.mainItemCheck.setChecked(todo.isDone)
listItemBinding.mainItemTxtMainCategory.setText(todo.mainCategory)
listItemBinding.mainItemTxtSubCategory.setText(todo.subCategory)
}
}
}
<!-- content_main.xml -->
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/background"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/purple_200">
<TextView
android:id="@+id/main_txt_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:text="@string/main_txt_date"
android:textColor="@color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/main_txt_complete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:text="@string/main_txt_complete"
android:textColor="@color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/main_txt_listSize"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/main_txt_listSize"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="8dp"
android:text="@string/main_txt_listSize"
android:textColor="@color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
참고
https://tutorialwing.com/android-recyclerview-using-kotlin-with-example/
https://developer.android.com/jetpack/androidx/releases/recyclerview
'경험들 > 토이 프로젝트' 카테고리의 다른 글
"할일관리 어플" #3 datepickerDialog (0) | 2022.04.06 |
---|---|
"할일관리 어플" #1 달력 만들기 (0) | 2022.03.29 |