Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- NPM
- window
- SSL
- GIT
- SQL
- SPC
- xPlatform
- 보조정렬
- MSSQL
- react
- Java
- es6
- JavaScript
- mybatis
- Spring
- hadoop
- Python
- Kotlin
- vaadin
- table
- Sqoop
- mapreduce
- Eclipse
- Android
- 공정능력
- R
- IntelliJ
- tomcat
- Express
- plugin
Archives
- Today
- Total
DBILITY
안드로이드 RecyclerView 기초 사용법 ( viewMoldel 적용 ) 본문
반응형
https://hyperrookie.tistory.com/730
이전 코드에 viewModel을 적용해 보았다.
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.launch
class TodoViewModel : ViewModel() {
private val _items = MutableStateFlow<List<Todo>>(emptyList())
val items: StateFlow<List<Todo>> = _items
private val dataList = mutableListOf<Todo>()
init {
for (idx in 1..100) {
dataList.add(Todo(idx,"do something $idx", System.currentTimeMillis()))
}
viewModelScope.launch {
_items.value = dataList
}
}
}
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.example.myrecyclerviewbyviewmodel.databinding.ActivityMainBinding
import com.example.myrecyclerviewbyviewmodel.databinding.ItemViewBinding
import kotlinx.coroutines.launch
import java.text.SimpleDateFormat
class MainActivity : AppCompatActivity() {
companion object {
private const val TAG = "LOG"
}
private val binding by lazy {
ActivityMainBinding.inflate(layoutInflater)
}
//private val viewModel:TodoViewModel by viewModels()
private lateinit var viewModel: TodoViewModel
private val dateFormatter = SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS")
private val todoListAdapter = TodoListAdapter { data->
Log.d(TAG, data.todo)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContentView(binding.root)
ViewCompat.setOnApplyWindowInsetsListener(binding.main) { v, insets ->
val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
insets
}
viewModel = ViewModelProvider(this)[TodoViewModel::class.java]
lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.STARTED) {
viewModel.items.collect {
Log.d(TAG, it.toString())
todoListAdapter.submitList(it)
}
}
}
with(binding) {
androidMainRecyclerView.apply {
layoutManager = LinearLayoutManager(root.context)
itemAnimator = null
adapter = todoListAdapter
}
}
}
inner class TodoListAdapter(private val onClickListener: (Todo) -> Unit) :
ListAdapter<Todo, TodoViewHolder>(TodoDiffItemCallback()) {
private lateinit var itemViewBinding: ItemViewBinding
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TodoViewHolder {
itemViewBinding =
ItemViewBinding.inflate(LayoutInflater.from(parent.context), parent, false)
return TodoViewHolder(itemViewBinding, onClickListener)
}
override fun onBindViewHolder(holder: TodoViewHolder, position: Int) {
holder.bind(getItem(position))
}
}
inner class TodoViewHolder(
private val itemViewBinding: ItemViewBinding,
private val onClickListener: (Todo) -> Unit
) :
RecyclerView.ViewHolder(itemViewBinding.root) {
fun bind(todo: Todo) {
with(itemViewBinding) {
itemViewId.text = todo.id.toString()
itemViewTodo.text = todo.todo
itemViewDatetime.text = dateFormatter.format(todo.datetime)
root.setOnClickListener {
onClickListener(todo)
}
}
}
}
inner class TodoDiffItemCallback : DiffUtil.ItemCallback<Todo>() {
override fun areItemsTheSame(oldItem: Todo, newItem: Todo): Boolean {
return oldItem.id == newItem.id
}
override fun areContentsTheSame(oldItem: Todo, newItem: Todo): Boolean {
return oldItem == newItem
}
}
}
ViewModelProvider를 사용하지 않고, private val viewModel:TodoViewModel by viewModels()처럼 사용하려면
fragment-ktx를 dependancy에 추가해야 한다.
반응형
'android > kotlin' 카테고리의 다른 글
안드로이드 RecyclerView 기초 사용법 ( viewMoldel update 적용 ) (0) | 2024.03.11 |
---|---|
kotlin reduce, fold (0) | 2024.03.11 |
안드로이드 RecyclerView 기초 사용법 (0) | 2024.03.05 |
안드로이드 enableEdgeToEdge,setOnApplyWindowInsetsListener (0) | 2024.03.05 |
안드로이드 spinner(combo) 사용법 (0) | 2024.03.04 |
Comments