【已解决】 使用 SubmitList 提交数据给 ListAdapter 就崩溃,问题请教
朋友们,请教一个从 recyclerview Adapter 迁移到 ListAdapter 产生的问题:
我一加上这句 submitList ,进入 mainActivity 界面就崩溃了,mainActivity是加载数据库所有的数据并展示在 recyclerview 里面。
不加 submitList 的时候,页面可以进去,但是数据没有初始化,界面是空的,上面初始化 adapter 的时候提交给 adapter 一个 empty list ,没有崩溃,一提交给他有数据的 list 就崩溃。
报错说是 UI 有问题,但是之前用 recyclerview adpter 的时候可以正常用,换到 list adapter 就不正常了。
使用 log 查看,实际上 submitList 这句是执行了的。
查看数据库,数据库那边是正常的。网上到处翻也没看到类似问题,想请大家帮我看一下,谢谢!
Fragment 代码:
class MainBlogItemListFragment : Fragment() {
private lateinit var mainBlogItemListRecyclerView: RecyclerView
private var adapter = MainBlogItemListAdapter()
private val mainBlogItemListViewModelInFragment by lazy {
ViewModelProvider(this).get(MainBlogItemListViewModel::class.java)
}
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view = inflater.inflate(R.layout.blog_item_list_fragment, container, false)
// 主界面的博客流展示
mainBlogItemListRecyclerView = view.findViewById(R.id.main_BlogItemList_RecyclerView)
mainBlogItemListRecyclerView.layoutManager = LinearLayoutManager(context)
adapter.submitList(emptyList())
mainBlogItemListRecyclerView.adapter = adapter
return view
}
// onViewCreated时,fragment 界面数据已经准备好了,防止观察的数据没有
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
// 直接观察 viewmodel里面的 查询到的所有blog的那个livedata
// viewLifecycleOwner 是 fragment,这个observer 与 这个fragment 同生共死
mainBlogItemListViewModelInFragment.blogItemListLiveData.observe(
viewLifecycleOwner, {
it?.let {
adapter = MainBlogItemListAdapter()
-----------------------------------------此句删掉不闪退,但是就是空白了,没有更新数据的功能----------------------------------------------------
adapter.submitList(it.toMutableList())
mainBlogItemListRecyclerView.adapter = adapter
}
}
)
}
// 让activity调用获取fragment实例
companion object {
fun newInstance(): MainBlogItemListFragment {
return MainBlogItemListFragment()
}
}
}
Adapter 代码:
class MainBlogItemListAdapter
: ListAdapter<BlogItem, MainBlogItemListViewHolder>(MainBlogItemListDiffUtil) {
// viewHolder
inner class MainBlogItemListViewHolder(view: View) : RecyclerView.ViewHolder(view) {
private val userName: TextView = itemView.findViewById(R.id.userName)
private val userAvatar: ImageView = view.findViewById(R.id.userAvatar)
private val blogTime: TextView = itemView.findViewById(R.id.blogTime)
private val blogSource: TextView = view.findViewById(R.id.blogSource)
private val blogContent: TextView = view.findViewById(R.id.blogContent)
private val blogImage: ImageView = view.findViewById(R.id.blogImage)
val isReblog: View? = view.findViewById(R.id.isReblogButton)
val comment: View? = view.findViewById(R.id.commentButton)
var isStar: View? = view.findViewById(R.id.isStarButton)
val isLike: View? = view.findViewById(R.id.isLikeButton)
private val moreAction: View? = view.findViewById(R.id.moreActionButton)
private val tabOfBlogComment: View? = view.findViewById(R.id.tabOfBlogComment)
private val commentRecyclerView: View? = view.findViewById(R.id.commentRecyclerView)
private val isReadFullContent: View? = view.findViewById(R.id.isReadFullContent)
private val blogFront: View? = view.findViewById(R.id.blogItem_front)
private val blogCenter: View? = view.findViewById(R.id.blogItem_center)
fun bind(blogItem: BlogItem) {
userName.text = blogItem.userName
... ... ... ...等
}
}
// adapter
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MainBlogItemListViewHolder {
val view = LayoutInflater.from(context).inflate(R.layout.blog_item, parent, false)
return MainBlogItemListViewHolder(view)
}
override fun onBindViewHolder(holder: MainBlogItemListViewHolder, position: Int) {
holder.bind(getItem(position))
}
object MainBlogItemListDiffUtil : DiffUtil.ItemCallback<BlogItem>() {
override fun areItemsTheSame(oldItem: BlogItem, newItem: BlogItem): Boolean {
// 借用主键来比较,看是不是同一个对象
return oldItem.blogId == newItem.blogId
}
override fun areContentsTheSame(oldItem: BlogItem, newItem: BlogItem): Boolean {
// 看各项内容是否相同
return (oldItem.blogContent == newItem.blogContent && oldItem.blogImage == newItem.blogImage &&
oldItem.comment == newItem.comment && oldItem.blogSource == newItem.blogSource &&
oldItem.userName == newItem.userName && oldItem.userAvatar == newItem.userAvatar &&
oldItem.blogTime == newItem.blogTime && oldItem.isReblog == newItem.isReblog &&
oldItem.isStar == newItem.isStar && oldItem.isLike == newItem.isLike &&
oldItem.isDelete == newItem.isDelete && oldItem.isDraft == newItem.isDraft)
}
}
}

debug 的时候截的图,新列表是5条数据。

<?xml version="1.0" encoding="utf-8"?>
// 用于在card外加边距,加了一个 linearlayout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:tools="http://schemas.android.com/tools"
android:background="@android:color/transparent"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="1.5dp"
android:paddingBottom="1.5dp"
app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior">
<com.google.android.material.card.MaterialCardView
style="?attr/materialCardViewElevatedStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:elevation="15dp"
app:cardBackgroundColor="#FFFFFF"
app:cardCornerRadius="@dimen/card_radius"
23行 ↓ , >标签在哪一行哪一行就报错
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="@+id/blogItem_front">
<de.hdodenhof.circleimageview.CircleImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:id="@+id/userAvatar"
tools:src="@drawable/avatar"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="15dp"
android:id="@+id/userName"
android:textStyle="bold"
tools:text="迟北"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="8dp"
android:id="@+id/blogTime" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:textSize="8dp"
android:id="@+id/blogSource" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/blogItem_center"
android:orientation="vertical"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_marginStart="5dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="15dp"
android:id="@+id/blogContent"
android:maxLines="5"
android:ellipsize="end"
tools:text="this is a sample"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="查看全文"
android:textColor="@color/background_basic_darker_2"
android:textSize="13dp"
android:id="@+id/isReadFullContent" />
</LinearLayout>
<ImageView
android:layout_width="match_parent"
android:layout_height="200dp"
android:scaleType="centerCrop"
android:layout_marginBottom="10dp"
android:id="@+id/blogImage"
tools:src="@drawable/pic1"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="@+id/imageButtonSet">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_baseline_repeat_24"
android:backgroundTint="#FFFFFF"
android:id="@+id/isReblogButton" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_outline_chat_bubble_outline_24"
android:backgroundTint="#FFFFFF"
android:id="@+id/commentButton"
android:visibility="visible"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_baseline_favorite_border_24"
android:backgroundTint="#FFFFFF"
android:id="@+id/isLikeButton"
android:visibility="gone"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/star_button"
android:backgroundTint="#FFFFFF"
android:id="@+id/isStarButton" />
<View
android:layout_width="0dp"
android:layout_height="30dp"
android:layout_weight="1"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_baseline_more_horiz_24"
android:backgroundTint="#FFFFFF"
android:id="@+id/moreActionButton" />
</LinearLayout>
<com.google.android.material.tabs.TabLayout
android:layout_width="wrap_content"
android:layout_height="30dp"
android:background="#FFFFFF"
android:id="@+id/tabOfBlogComment">
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="评论(5)" />
</com.google.android.material.tabs.TabLayout>
<androidx.recyclerview.widget.RecyclerView
android:layout_margin="10dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/commentRecyclerView"
app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior">
</androidx.recyclerview.widget.RecyclerView>
</LinearLayout>
</com.google.android.material.card.MaterialCardView>
</LinearLayout>
Adadpter的xml文件发出来看看,感觉是cardview的问题,cardview只能包含一个子控件,有多个控件的话,需要在外面添加一个父容器(如linearLayout),
根据提示,看一下你的那个布局23行代码是啥,顺着这个去找
对着官方的例子,不知道改了什么,突然不崩溃了?现在刷新确实是只刷新变了的部分(因为可以看到改变的那一条的闪烁),问题是向列表添加数据的时候,不会聚焦到列表顶端。(之前全局刷新的时候,刷新一下返回顶部一次),容我看看解决办法。
我怀疑是这里的问题,