@@ -24,9 +24,9 @@ import androidx.appcompat.app.AppCompatActivity
24
24
import androidx.lifecycle.lifecycleScope
25
25
import androidx.recyclerview.widget.ItemTouchHelper
26
26
import androidx.recyclerview.widget.RecyclerView
27
- import kotlinx.android.synthetic.main.activity_main.*
28
27
import kotlinx.coroutines.flow.collectLatest
29
28
import kotlinx.coroutines.launch
29
+ import paging.android.example.com.pagingsample.databinding.ActivityMainBinding
30
30
31
31
/* *
32
32
* Shows a list of Cheeses, with swipe-to-delete, and an input field at the top to add.
@@ -35,15 +35,18 @@ import kotlinx.coroutines.launch
35
35
* is updated automatically using paging components.
36
36
*/
37
37
class MainActivity : AppCompatActivity () {
38
+ lateinit var binding: ActivityMainBinding
39
+ private set
38
40
private val viewModel by viewModels<CheeseViewModel >()
39
41
40
42
override fun onCreate (savedInstanceState : Bundle ? ) {
41
43
super .onCreate(savedInstanceState)
42
- setContentView(R .layout.activity_main)
44
+ binding = ActivityMainBinding .inflate(layoutInflater)
45
+ setContentView(binding.root)
43
46
44
47
// Create adapter for the RecyclerView
45
48
val adapter = CheeseAdapter ()
46
- cheeseList.adapter = adapter
49
+ binding. cheeseList.adapter = adapter
47
50
48
51
// Subscribe the adapter to the ViewModel, so the items in the adapter are refreshed
49
52
// when the list changes
@@ -60,7 +63,7 @@ class MainActivity : AppCompatActivity() {
60
63
// enable the items to swipe to the left or right
61
64
override fun getMovementFlags (recyclerView : RecyclerView ,
62
65
viewHolder : RecyclerView .ViewHolder ): Int =
63
- makeMovementFlags(0 , ItemTouchHelper .LEFT or ItemTouchHelper .RIGHT )
66
+ makeMovementFlags(0 , ItemTouchHelper .LEFT or ItemTouchHelper .RIGHT )
64
67
65
68
override fun onMove (recyclerView : RecyclerView , viewHolder : RecyclerView .ViewHolder ,
66
69
target : RecyclerView .ViewHolder ): Boolean = false
@@ -72,32 +75,32 @@ class MainActivity : AppCompatActivity() {
72
75
viewModel.remove(it)
73
76
}
74
77
}
75
- }).attachToRecyclerView(cheeseList)
78
+ }).attachToRecyclerView(binding. cheeseList)
76
79
}
77
80
78
81
private fun addCheese () {
79
- val newCheese = inputText.text.trim()
82
+ val newCheese = binding. inputText.text.trim()
80
83
if (newCheese.isNotEmpty()) {
81
84
viewModel.insert(newCheese)
82
- inputText.setText(" " )
85
+ binding. inputText.setText(" " )
83
86
}
84
87
}
85
88
86
89
private fun initAddButtonListener () {
87
- addButton.setOnClickListener {
90
+ binding. addButton.setOnClickListener {
88
91
addCheese()
89
92
}
90
93
91
94
// when the user taps the "Done" button in the on screen keyboard, save the item.
92
- inputText.setOnEditorActionListener { _, actionId, _ ->
95
+ binding. inputText.setOnEditorActionListener { _, actionId, _ ->
93
96
if (actionId == EditorInfo .IME_ACTION_DONE ) {
94
97
addCheese()
95
98
return @setOnEditorActionListener true
96
99
}
97
100
false // action that isn't DONE occurred - ignore
98
101
}
99
102
// When the user clicks on the button, or presses enter, save the item.
100
- inputText.setOnKeyListener { _, keyCode, event ->
103
+ binding. inputText.setOnKeyListener { _, keyCode, event ->
101
104
if (event.action == KeyEvent .ACTION_DOWN && keyCode == KeyEvent .KEYCODE_ENTER ) {
102
105
addCheese()
103
106
return @setOnKeyListener true
0 commit comments