Идея моего проекта состоит в том, чтобы щелкнуть в одном RecyclerView в MainActivity и после того, как откроется одно диалоговое окно с другим RiderView. Ошибка в том, что мой recyclerView не обновляется в диалоге
MainActivity:
private fun openDialog(postModel: PostModel){
CommentDialogFragment(postModel).show(supportFragmentManager, "dialog")
}
CommentDialogFragment:
class CommentDialogFragment(postSelected: PostModel): AppCompatDialogFragment() {
private var mCommentsList: MutableList<CommentModel> = arrayListOf()
private val mCommentServiceRequest = CommentServiceRequest()
private lateinit var mContext: Context
private lateinit var mRecyclerView: RecyclerView
private var postModel: PostModel = postSelected
private lateinit var mCommentListAdapter: CommentListAdapter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setStyle(STYLE_NORMAL, R.style.DialogStyle)
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
val viewGroup = inflater.inflate(R.layout.comment, container)
mContext = inflater.context
mCommentListAdapter = CommentListAdapter(mCommentsList)
mRecyclerView = viewGroup.findViewById(R.id.recyclerViewComments)
setupCommentObserver(postModel.id)
loadCommentRecyclerView()
dialog!!.setTitle("${mContext.getString(R.string.comments)} ${postModel.title}")
return viewGroup
}
private fun setupCommentObserver(postId: Int){
mCommentServiceRequest.searchCommentsFromAPI(postId)
.observe(this, Observer { comments ->
if (comments != null){
mCommentsList = comments.toMutableList()
mCommentListAdapter.setData(mCommentsList)
}
})
}
private fun loadCommentRecyclerView() {
mCommentsList.clear()
mRecyclerView.setHasFixedSize(true)
mRecyclerView.adapter = mCommentListAdapter
mRecyclerView.layoutManager = LinearLayoutManager(mContext)
}}
CommentListAdapter:
class CommentListAdapter(private var commentList: MutableList<CommentModel>):
RecyclerView.Adapter<CommentViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CommentViewHolder {
val inflate = LayoutInflater.from(parent.context)
val view = inflate.inflate(R.layout.comment_list , parent, false)
return CommentViewHolder(view)
}
override fun getItemCount(): Int {
return commentList.count()
}
override fun onBindViewHolder(holder: CommentViewHolder, position: Int) {
holder.bindTask(commentList[position])
}
fun setData(list: MutableList<CommentModel>){
this.commentList = list
this.notifyDataSetChanged()
}}
Я сделал то же самое в другом проекте без использования notifyDataSetChanged () только с использованием обратных вызовов и работал, поэтому я попытался и здесь, но не сработало, Я думаю, что диалог является проблемой