【问题标题】:Trouble with Recyclerview. recyclerview.xml must no be nullRecyclerview 的问题。 recyclerview.xml 不能为空
【发布时间】:2020-10-15 16:05:57
【问题描述】:

我有带有 recyclerview 的 xml,id = list_chat

<androidx.constraintlayout.widget.ConstraintLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   tools:context=".ui.chats.ChatsFragment">

   <androidx.recyclerview.widget.RecyclerView
       android:id="@+id/list_chat"
       android:layout_width="match_parent"
       android:layout_height="match_parent">
   </androidx.recyclerview.widget.RecyclerView>

</androidx.constraintlayout.widget.ConstraintLayout> 

我的 Fragment 将是 recyclerview 与函数 uploadList 将数据放入我的数组中用于 recyclerView

class ChatsFragment : Fragment() {

    var list: MutableList<Chat> = ArrayList()
    lateinit var adapter: ChatListAdapter
    lateinit var manager: RecyclerView.LayoutManager


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        uploadList()
        Log.e("TAG", list.toString())

    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val view: View = inflater.inflate(R.layout.fragment_chats, container, false)
        uploadList()
        manager = LinearLayoutManager(activity)
        list_chat.layoutManager = manager
        adapter = ChatListAdapter(list)
        list_chat.adapter = adapter
        return view
    }
    
    private fun uploadList(){
        list.add(Chat("11111", "11111"))
        list.add(Chat("11222221", "133311"))
        list.add(Chat("1122221", "114444411"))
        list.add(Chat("112222211", "5555555"))
    }
}

带有列表项的xml。 ImageView 现在无关紧要。只需要两个textView

<?xml version="1.0" encoding="utf-8"?>
<layout
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android">

<LinearLayout
    android:id="@+id/item_list"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <de.hdodenhof.circleimageview.CircleImageView
        android:id="@+id/chatImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_launcher"/>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_marginStart="5sp">
        <TextView
            android:id="@+id/nameChat"
            android:text="2222222222222"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        <TextView
            android:id="@+id/infoChat"
            android:text="11111111"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    </LinearLayout>
</LinearLayout>
</layout>

还有我的自定义适配器

class ChatListAdapter(private val myDataset: MutableList<Chat>) :RecyclerView.Adapter<ChatListAdapter.ViewHolder>() {



    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val layoutInflater = LayoutInflater.from(parent.context)
            .inflate(R.layout.item_list, parent, false)
        return ViewHolder(layoutInflater)
    }


    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.bind(myDataset[position])

    }


    override fun getItemCount() = myDataset.size

    class ViewHolder(v: View) : RecyclerView.ViewHolder(v){
        private val nameChat: TextView = itemView.findViewById(R.id.nameChat)
        private val infoChat: TextView = itemView.findViewById(R.id.infoChat)

        fun bind(chat: Chat){
            nameChat.text = chat.name
            infoChat.text = chat.info
        }

    }
}

我在list_chat.layoutManager = manager这里抓到list_chat.layoutManager = manager

帮帮我,我只有在fragment_chats 中有list_chat,所以它是正确的recyclerview。 因为请帮助我,我不知道是什么问题

【问题讨论】:

    标签: android kotlin android-fragments android-recyclerview android-adapter


    【解决方案1】:

    在 onViewCreated() 方法中初始化布局管理器和适配器

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
            super.onViewCreated(view, savedInstanceState)
             uploadList()
            manager = LinearLayoutManager(activity)
            list_chat.layoutManager = manager
            adapter = ChatListAdapter(list)
            list_chat.adapter = adapter
            })
    

    【讨论】:

    • 它有帮助(为什么是这个函数?),但出现新错误,Binary XML file line #2: Error inflating class layout
    • 由于你使用的是DataBinding,你不能直接调用inflate()。相反,您应该使用ItemListBinding.inflate(inflater, parent, false)
    • 我成功了,泰本)
    【解决方案2】:

    您是否使用kotlinx.android.synthetic 来查找您的观点?如果是这样,这些直到onCreateView() 返回之后才有效,因此您不能在onCreateView() 内使用它们

    将您的代码移动到onViewCreated(),或切换到从onCreateView() 内部手动调用view.findViewById()

    【讨论】:

      【解决方案3】:

      方法一:

      创建一个recyclerview object

      val rv = view.findViewById(your rv id)
      

      并将布局管理器和适配器分配给它。

      方法二:

      在你的布局文件中,在 recyclerview 中添加这一行:

      <androidx.recyclerview.widget.RecyclerView
              <!-- your default lines -->
              app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
       />
      

      并在onViewCreated method 中分配适配器。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-02-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多