我在一个片段中有一个recyclerview,并且已经实现了滑动以将其删除。
请在下面找到代码:
fragment.kt
private lateinit var mBinding: FragmentToDoBinding
private lateinit var mToDoViewModel: ToDoViewModel
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
// Inflate the layout for this fragment
mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_to_do, container, false)
configureScreenAppearance()
val application = requireNotNull(this.activity).application
val dataSource = LifestyleDatabase.getInstance(application).toDoDao
val viewModelFactory = ToDoViewModelFactory(dataSource, application)
mToDoViewModel = ViewModelProviders.of(this, viewModelFactory).get(ToDoViewModel::class.java)
mBinding.lifecycleOwner = this
val adapter = ToDoAdapter()
mBinding.RecyclerViewFromFragmentToDoMain.adapter = adapter
mToDoViewModel.toDos.observe(viewLifecycleOwner, Observer {
it?.let { x ->
//Update the UI and determine whether recyclerview should be visible or not
updateUI(x.isNotEmpty())
adapter.submitList(x)
}
})
val swipeHandler = object : SwipeToCallback(requireContext()) {
override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
//Delete the item
}
}
val itemTouchHelper = ItemTouchHelper(swipeHandler)
itemTouchHelper.attachToRecyclerView(mBinding.RecyclerViewFromFragmentToDoMain)
return mBinding.root
}
ToDoAdapter.kt
class ToDoAdapter : ListAdapter<ToDo, ToDoAdapter.ViewHolder>(ToDoDiffCallback()) {
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val toDo = getItem(position)
holder.bind(toDo)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
return ViewHolder.from(parent)
}
class ViewHolder private constructor(val binding: CustomRecyclerviewListTodoBinding): RecyclerView.ViewHolder(binding.root){
fun bind(toDo: ToDo) {
binding.myToDo = toDo
binding.executePendingBindings()
}
companion object {
fun from(parent: ViewGroup): ViewHolder {
val layoutInflater = LayoutInflater.from(parent.context)
val binding = CustomRecyclerviewListTodoBinding.inflate(layoutInflater, parent, false)
return ViewHolder(binding)
}
}
}
}
class ToDoDiffCallback: 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
}
}
我需要删除在此方法中刷过的行:
val swipeHandler = object : SwipeToCallback(requireContext()) {
override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
//Delete the item
}
}
但是我似乎找不到解决方法。有人可以帮忙一些工作代码吗?
我试图寻找一些方法来删除适配器中的项目,但找不到任何方法。
请帮助。
答案 0 :(得分:0)
您可以使用onerror
ItemTouchHelper.SimpleCallBack
然后只有你写这样的东西。
ItemTouchHelper mIth = new ItemTouchHelper(
new ItemTouchHelper.SimpleCallback(ItemTouchHelper.UP | ItemTouchHelper.DOWN,
ItemTouchHelper.LEFT) {
public boolean onMove(RecyclerView recyclerView,
ViewHolder viewHolder, ViewHolder target) {
}
public void onSwiped(ViewHolder viewHolder, int direction) {
// remove from adapter
}
});