【发布时间】:2023-03-14 10:45:02
【问题描述】:
我有一个RecyclerView,它显示从MutableList modSamplePhrases 读取的短语列表。当点击一个项目时,会弹出一个DialogFragment,它会显示列表项目中的短语并允许对其进行编辑。
编辑完成后,新文本通过getDataFromDialog() 函数返回到RecyclerFragment。
Logcat 显示基础数据更新成功(所以.clear() 和.addAll() 没有区别)但对notifyItemChanged(pos) 或notifyDataSetChanged() 的调用仍然没有任何作用。
class PhraseRecyclerFragment : Fragment(){
private var modSamplePhrases = PhraseData().samplePhrases
private var phraseListAdapter = PhraseListAdapter(modSamplePhrases)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
retainInstance = true
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? =
inflater.inflate(R.layout.fragment_recycler, container, false)
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
list_recycler_view.apply {
layoutManager = LinearLayoutManager(activity)
adapter = phraseListAdapter
}
list_recycler_view.addOnItemClickListener(object: OnItemClickListener {
override fun onItemClicked(position: Int, view: View) {
val newFragment = PhraseDialogFragment()
val args = Bundle()
args.putInt("POSITION", position)
args.putString("CONTENT", modSamplePhrases[position].sample)
newFragment.arguments = args
val fm = fragmentManager
newFragment.show(fm, "STF")
}
})
}
fun getDataFromDialog(pos: Int, dialogText: String) {
modSamplePhrases[pos].sample = dialogText
println("item " + pos + ": " + modSamplePhrases[pos].sample)
phraseListAdapter.notifyDataSetChanged()
}
我已经尝试在修改数据后重新创建适配器
phraseListAdapter = PhraseListAdapter(modSamplePhrases)
我还尝试获取对 RecyclerView 的引用并重置其上的适配器
recyclerView?.adapter = phraseListAdapter
但还是一无所获。
PhraseListAdapter代码:
class PhraseViewHolder(inflater: LayoutInflater, parent: ViewGroup) :
RecyclerView.ViewHolder(inflater.inflate(R.layout.list_item, parent, false)) {
private var indexView: TextView? = null
private var sampleView: TextView? = null
init {
indexView = itemView.findViewById(R.id.text_index)
sampleView = itemView.findViewById(R.id.text_sample)
}
fun bind(phrase: Phrase) {
indexView?.text = phrase.sample
sampleView?.text = phrase.index.toString()
}
}
class PhraseListAdapter (private val list: MutableList<Phrase>) : RecyclerView.Adapter<PhraseViewHolder>(){
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PhraseViewHolder {
val inflater = LayoutInflater.from(parent.context)
return PhraseViewHolder(inflater, parent)
}
override fun onBindViewHolder(holder: PhraseViewHolder, position: Int) {
val phrase: Phrase = list[position]
holder.bind(phrase)
}
override fun getItemCount(): Int = list.size
}
´´´
【问题讨论】:
-
可以在这里添加 PhraseListAdapter 的代码吗?
-
感谢您的快速响应。我已经添加了适配器代码。
-
你重新创建了适配器,但是recyclerview指向了旧的istance,所以显然通知新实例不会做任何事情
-
就像@gpunto 指出的那样,您正在重新创建适配器实例,但没有将其设置为recyclerview。您可以将其设置为新的并省略 notifydatasetchanged() 调用。您可以尝试在适配器内部有一个更新方法,在将数据添加到数据列表后调用更新方法并在其中调用 notifiydatasetchanged()。我认为这应该可行。
-
我明白重新创建适配器的意义。我想我把那条线留在帖子里弄混了水,所以我把它去掉了。我还尝试重置 RecyclerView 上的适配器以及两个选项,但实际上,两者都不是必需的。我还尝试将更新方法放在适配器内,但结果相同 - 没有。尽管进行了广泛的搜索,但我还没有在 Kotlin 的任何地方找到 notifyDatasetChanged() 的工作示例。
标签: android kotlin android-recyclerview notifydatasetchanged notifyitemchanged