0

Hello i am using a recyclerView to swipe and call for a Dialog fragment enter image description here

The the idea is that when i dismiss the dialog Fragment the raw come back to the original place but the problem is that it return, just after the dialog is opened. enter image description here

So i do not how to call notifyItemChanged inside the dialogFragment, or implement the dialogFragment.setOnDismissListener because when i override i dont know how to pass to the function the adapter to call notifyItemChanged i

This is my code

class Fragmento : Fragment(), RecyclerAdapter.ClickListener { // TODO: Rename and change types of parameters

private lateinit var adapter :RecyclerAdapter
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    arguments?.let {

    }
}

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                          savedInstanceState: Bundle?): View? {
    // Inflate the layout for this fragment
    val view =inflater.inflate(R.layout.fragment_fragmento, container,false)

    initRecyclerView(view)
    return view

}

private fun initRecyclerView(view: View) {
    val recyclerView = view.findViewById<RecyclerView>(R.id.recyclerView)
    recyclerView.layoutManager=LinearLayoutManager(activity)
    adapter = RecyclerAdapter()
    recyclerView.adapter=adapter
    val itemSwipe=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) {
            showDialog(viewHolder as RecyclerAdapter.ViewHolder)
            var dialog = DialogFragment()
            dialog.show(childFragmentManager,"dialog")
            adapter.notifyItemChanged(viewHolder.adapterPosition)
        }

    }
    val swap =ItemTouchHelper(itemSwipe)
    swap.attachToRecyclerView(recyclerView)

}

private fun showDialog(viewHolder: RecyclerAdapter.ViewHolder){
    val builder= AlertDialog.Builder(activity)
    builder.setTitle("DeleteItem")


}


companion object {
    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     */
    // TODO: Rename and change types and number of parameters
    @JvmStatic
    fun newInstance() =
            Fragmento().apply {
                arguments = Bundle().apply {

                }
            }
}

}

and for the DialogFragment

class DialogFragment: DialogFragment(){

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
  
    var rootView: View = inflater.inflate(R.layout.dialog_fragment_noticias, container, false)
    return rootView
}

override fun onDismiss(dialog: DialogInterface) {
    super.onDismiss(dialog)

}

}

1 Answers1

0

If I have understand the problem here is that you want to call notifyItemChanged() when your dialog is dismiss without setOnDismissListener.

If you want to do that you can simply add a callback on your DialogFragment and use it in your recyclerView.

class DialogFragment(dismiss: () -> Unit): DialogFragment(){

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
  
    var rootView: View = inflater.inflate(R.layout.dialog_fragment_noticias, container, false)
    return rootView
}

override fun onDismiss(dialog: DialogInterface) {
    super.onDismiss(dialog)
    dismiss()
}
}

When you are creating the DialogFragment you can use the callback :

override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
            showDialog(viewHolder as RecyclerAdapter.ViewHolder)
            var dialog = DialogFragment() {
               //here you are notified when the dialog is dismiss
               adapter.notifyItemChanged(viewHolder.adapterPosition)
            }
            dialog.show(childFragmentManager,"dialog")
            adapter.notifyItemChanged(viewHolder.adapterPosition)
        }
Jolan DAUMAS
  • 1,078
  • 1
  • 5
  • 9
  • it didn't work, notifyitemchanged is a function to return the row to his original position, maybe is needed something to call for the callback, i tried to look for onDismiss, or dialog.onDismiss(), inside the dialog the declaration, but no results :( – Rodrigo Barboza Aug 21 '21 at 12:48
  • Maybe the name of the callback was wrong because there is alreay a onDimiss method in the dialog component I'm not sure about your problem. You want to call notifyItemChanged when you dismiss your dialog right ? – Jolan DAUMAS Aug 21 '21 at 14:51
  • yes, that is exactly what i was looking, i put the code as you say in the example with the callback (dismiss: () -> Unit), and the rest ,but the row keep returning to the original position before i dimiss the Dialog, just like the second screenshot that i uploaded – Rodrigo Barboza Aug 21 '21 at 15:36