gpt4 book ai didi

android - E/RecyclerView : No adapter attached; skipping layout in Android, Kotlin

转载 作者:行者123 更新时间:2023-12-02 13:26:38 26 4
gpt4 key购买 nike

我已使用 Refrofit,MVVM,DataBinding,协程 Kotlin 在我的应用程序中实现了RecyclerView。相同的代码在另一个片段中可以正常工作,但在此处不行。
*注意:翻新功能可以成功返回commentsList,但唯一的问题是在recyclerView中显示列表。

  override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
val api = ApiRepository()
factory = CommentsViewModelFactory(api)
viewModel = ViewModelProvider(this, factory).get(CommentsViewModel::class.java)
viewModel.getComments(requireActivity())
viewModel.commentsList.observe(viewLifecycleOwner, Observer { comments ->
rvComment.also {
it.layoutManager = LinearLayoutManager(requireContext())
it.setHasFixedSize(true)
if (comments != null) {
it.adapter = HomeServicesCommentsAdapter(comments, this)
}
}

})
}
ViewModel看起来像这样,我将注释声明为MutableLiveData,它成功返回了数据,但唯一的问题是适配器附件。
class CommentsViewModel(private val repository: ApiRepository) : ViewModel() {
var userComment: String? = null

private val comments = MutableLiveData<List<Comment>>()
private lateinit var job: Job

val commentsList: MutableLiveData<List<Comment>>
get() = comments

fun getComments(context: Context) {
job = CoroutinesIO.ioThenMain(
{
repository.getServices(context)
}, {
for (i in it!!.data.data)
comments.value = i.comments
}


)
}
这是适配器的实现
class HomeServicesCommentsAdapter(
private val comments: List<Comment>,
private val listenerService: RvListenerServiceComments
) : RecyclerView.Adapter<HomeServicesCommentsAdapter.ServicesViewHolder>() {

override fun getItemCount() = comments.size


override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) =
ServicesViewHolder(
DataBindingUtil.inflate(
LayoutInflater.from(parent.context),
R.layout.custom_comment_layout,
parent,
false
)
)

override fun onBindViewHolder(holder: ServicesViewHolder, position: Int) {
holder.recyclerViewServicesBinding.comments = comments[position]
notifyDataSetChanged()
}

class ServicesViewHolder(
val recyclerViewServicesBinding: CustomCommentLayoutBinding
) : RecyclerView.ViewHolder(recyclerViewServicesBinding.root)

}
让我知道您是否需要xml布局文件。

最佳答案

与其在观察数据时在运行时提供布局管理器,不如说是,
在xml内定义layoutmanager
例如:

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rvNews"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:nestedScrollingEnabled="false"
tools:listitem="@layout/item_your_layout"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />
从观察者中删除以下几行
it.layoutManager = LinearLayoutManager(requireContext())
编辑:
观察数据时不要创建适配器实例,因为观察数据不在MainThread上,因此请确保在 MainThread上设置数据
val adapter = HomeServicesCommentsAdapter(arrayListOf(), this)
rvComment?.adapter = adapter
viewModel.getComments(requireActivity())
viewModel.commentsList.observe(viewLifecycleOwner, Observer { comments ->
comments?.let{adapter.setData(comments)}//define setData(list:ArrayList<Comments>) method in your adapter

})
HomeServicesCommentsAdapter.kt:
........
private var mObjects: MutableList<Comment>? = ArrayList()// top level declaration

fun setData(objects: List<Comment>?) {
this.mObjects = objects as MutableList<Comment>
this.notifyDataSetChanged()
}
......

关于android - E/RecyclerView : No adapter attached; skipping layout in Android, Kotlin ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63788759/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com