1 year ago
#389040
Dominik
How to set up ItemTouchHelper with RoomDB?
So i want to make a To-Do app. I want to make CardView
added to recyclerView
swipeable. I used ItemTouchHelper
but it does't work. Here is my code
MainActivity.kt
class MainActivity : AppCompatActivity() {
private val rotateOpen: Animation by lazy { AnimationUtils.loadAnimation(this,
R.anim.rotate_open_anim) }
private val rotateClose: Animation by lazy { AnimationUtils.loadAnimation(this,
R.anim.rotate_close_anim) }
private val fromBottom: Animation by lazy { AnimationUtils.loadAnimation(this,
R.anim.from_bottom_anim) }
private val toBottom: Animation by lazy { AnimationUtils.loadAnimation(this,
R.anim.to_bottom_anim) }
private var clicked = false
private var dialogView: View? = null
private lateinit var mTaskViewModel: TaskViewModel
private val adapter by lazy { TaskRecycleAdapter() }
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
add_btn?.setOnClickListener {
onAddButtonClicked()
}
edit_btn?.setOnClickListener {
Toast.makeText(this, "Edit Button Clicked", Toast.LENGTH_SHORT).show()
}
delete_btn?.setOnClickListener {
Toast.makeText(this, "Delete Button Clicked", Toast.LENGTH_SHORT).show()
}
add_task_btn.setOnClickListener {
showDialog()
}
recycleTask.layoutManager = LinearLayoutManager(this)
recycleTask.adapter = adapter
mTaskViewModel = ViewModelProvider(this)[TaskViewModel::class.java]
mTaskViewModel.readAllData.observe(this) { user ->
adapter.setData(user)
}
ItemTouchHelper(object: ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT){
override fun onMove(
recyclerView: RecyclerView,
viewHolder: RecyclerView.ViewHolder,
target: RecyclerView.ViewHolder
): Boolean {
return false
}
override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
mTaskViewModel.deleteFromDatabase(adapter.getTaskAt(viewHolder.adapterPosition))
}
})
}
private fun showDialog() {
dialogView = layoutInflater.inflate(R.layout.task_add_dialog, null)
val dialog = Dialog(this)
val task = dialogView?.findViewById<EditText>(R.id.taskAdd)
val taskDescription = dialogView?.findViewById<EditText>(R.id.taskDescriptionAdd)
dialogView?.findViewById<Button>(R.id.add_data_btn)?.setOnClickListener {
if(task?.text?.isEmpty() == true || taskDescription?.text?.isEmpty() == true){
Toast.makeText(this, "Dodaj poprawne dane", Toast.LENGTH_LONG).show()
}else{
val addTaskToDatabase = task?.text.toString()
val addTaskDescToDatabase = taskDescription?.text.toString()
val taskAddToDatabase = data(
0,
addTaskToDatabase,
addTaskDescToDatabase
)
mTaskViewModel.addTask(taskAddToDatabase)
Toast.makeText(this, "Dodano!", Toast.LENGTH_SHORT).show()
}
}
dialog.setContentView(dialogView!!)
dialog.show()
}
private fun onAddButtonClicked() {
setVisibility(clicked)
setAnimation(clicked)
clicked = !clicked
}
private fun setVisibility(clicked: Boolean) {
if (!clicked){
edit_btn.visibility = View.VISIBLE
delete_btn.visibility = View.VISIBLE
add_task_btn.visibility = View.VISIBLE
}else{
edit_btn.visibility = View.INVISIBLE
delete_btn.visibility = View.INVISIBLE
add_task_btn.visibility = View.INVISIBLE
}
}
private fun setAnimation(clicked: Boolean) {
if(!clicked){
add_btn.startAnimation(rotateOpen)
edit_btn.startAnimation(fromBottom)
delete_btn.startAnimation(fromBottom)
add_task_btn.startAnimation(fromBottom)
}else{
add_btn.startAnimation(rotateClose)
edit_btn.startAnimation(toBottom)
delete_btn.startAnimation(toBottom)
add_task_btn.startAnimation(toBottom)
}
}
}
TaskRecyclerAdapter.kt
class TaskRecycleAdapter: RecyclerView.Adapter<TaskRecycleAdapter.MyViewHolder>() {
private var list = emptyList<data>()
inner class MyViewHolder(binding: TaskHolderBinding): RecyclerView.ViewHolder(binding.root)
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
return MyViewHolder(TaskHolderBinding.inflate(LayoutInflater.from(parent.context), parent, false))
}
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
val item = list[position]
holder.itemView.findViewById<TextView>(R.id.task).text = item.task
holder.itemView.findViewById<TextView>(R.id.taskDescription).text = item.task_Details
}
override fun getItemCount(): Int {
return list.size
}
fun setData(newList: List<data>) {
val diffUtil = DiffUtil(list, newList )
val diffResults = calculateDiff(diffUtil)
list = newList
diffResults.dispatchUpdatesTo(this)
}
fun getTaskAt(position: Int): data {
return list[position]
}
}
Repository.kt
package eu.tuto.wooftask.database
import androidx.lifecycle.LiveData
import kotlinx.coroutines.flow.Flow
class Repository(private val addTaskDao: taskDao) {
val readAllData: LiveData<List<data>> = addTaskDao.readAllData()
suspend fun addTask(user: data){
addTaskDao.addTask(user)
}
fun deleteFromDatabase(user: data){
addTaskDao.deleteFromDatabase(user)
}
}
TaskDao.kt
package eu.tuto.wooftask.database
import androidx.lifecycle.LiveData
import androidx.room.Dao
import androidx.room.Delete
import androidx.room.Insert
import androidx.room.Query
@Dao
interface taskDao {
@Insert
suspend fun addTask(user: data)
@Query("Select * From task_table order by id asc")
fun readAllData(): LiveData<List<data>>
@Delete
fun deleteFromDatabase(user: data)
}
android
kotlin
android-recyclerview
android-room
android-cardview
0 Answers
Your Answer