Recycler View 和 DialogFragment 苦苦挣扎(Kotlin)
Posted
技术标签:
【中文标题】Recycler View 和 DialogFragment 苦苦挣扎(Kotlin)【英文标题】:Recycler View and DialogFragment struggling (Kotlin) 【发布时间】:2021-10-22 05:52:06 【问题描述】:您好,我正在使用 recyclerView 滑动并调用 Dialog 片段
这个想法是,当我关闭对话框片段时,原始回到原来的地方 但问题是它会在对话框打开后返回。
所以我不知道如何在 dialogFragment 中调用 notifyItemChanged,或者实现 dialogFragment.setOnDismissListener,因为当我重写时,我不知道如何将适配器传递给函数以调用 notifyItemChanged i
这是我的代码
类 Fragmento : Fragment(), RecyclerAdapter.ClickListener // TODO:重命名和更改参数类型
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
对于 DialogFragment
类 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】:如果我明白这里的问题是您想在没有 setOnDismissListener 的情况下关闭对话框时调用 notifyItemChanged()。
如果你想这样做,你可以简单地在你的 DialogFragment 上添加一个回调并在你的 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()
当您创建 DialogFragment 时,您可以使用回调:
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)
【讨论】:
它没有用,notifyitemchanged 是一个将行返回到原始位置的函数,可能需要调用回调的东西,我试图寻找 onDismiss 或 dialog.onDismiss() ,在对话框内声明,但没有结果:( 也许回调的名称是错误的,因为对话框组件中已经有一个 onDimiss 方法我不确定你的问题。您想在关闭对话框时调用 notifyItemChanged 吗? 是的,这正是我一直在寻找的,我把你在示例中所说的代码与回调 (dismiss: () -> Unit) 和其余部分放在一起,但行继续返回回到我关闭对话框之前的原始位置,就像我上传的第二个屏幕截图一样以上是关于Recycler View 和 DialogFragment 苦苦挣扎(Kotlin)的主要内容,如果未能解决你的问题,请参考以下文章