如何将适配器连接到我的片段?

How can I attach an adapter to my fragment?

我在 SDK 29 中使用 Kotlin。

我想在我的片段中创建一个 recyclerView。当我 运行 设备时,它没有崩溃,片段出现但没有 Recycler View 并且我有以下错误:

E/RecyclerView:没有连接适配器;跳过布局

这是我的代码:

适配器

导入:

import android.content.Context
import android.text.format.DateUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.example.givenaskv1.R
import kotlinx.android.synthetic.main.item_post.view.*

代码:

class PostsAdapter (val context: Context, val posts : List<Post>) :
    RecyclerView.Adapter<PostsAdapter.ViewHolder>() {

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

    override fun getItemCount() = posts.size
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.bind(posts[position])
    }

    inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        fun bind(post: Post) {
            itemView.tvUsername.text = post.user?.firstName
            itemView.tvDescription.text = post.description
            Glide.with(context).load(post.imageUrl).into(itemView.ivPost)
            itemView.tvRelativeTime.text = DateUtils.getRelativeTimeSpanString(post.creationTimeMs)
        }
    }
}

片段

导入:

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.navigation.Navigation
import com.example.givenaskv1.R
import com.google.android.gms.maps.GoogleMap
import com.google.android.gms.maps.OnMapReadyCallback
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
import com.google.firebase.firestore.FirebaseFirestore
import kotlinx.android.synthetic.main.fragment_page_profil.*

代码:

class FragmentPageProfil : BottomSheetDialogFragment(), OnMapReadyCallback {
    private lateinit var googleMap: GoogleMap
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        readFireStoreData()

    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_page_profil, container, false)

        readFireStoreData()
    }




    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)




        btnProfilOption.setOnClickListener {
            val action = FragmentPageProfilDirections.actionFragmentPageProfil2ToProfilOption()
            Navigation.findNavController(it).navigate(action)
        }


        retourBtnprofil.setOnClickListener {
            val action = FragmentPageProfilDirections.actionFragmentPageProfil2ToNotificationFragment()
            Navigation.findNavController(it).navigate(action)
        }

        btnCalendar.setOnClickListener {
            val action = FragmentPageProfilDirections.actionFragmentPageProfil2ToFragmentCalendrier()
            Navigation.findNavController(it).navigate(action)
        }
        mapProfil.onCreate(savedInstanceState)
        mapProfil.onResume()
        mapProfil.getMapAsync(this)
    }
     override fun onMapReady(map: GoogleMap?) {
        map?.let {
            googleMap = it
        }
    }

    fun readFireStoreData() {
        val db = FirebaseFirestore.getInstance()
        db.collection("users")
            .get()
            .addOnCompleteListener {

                val result: StringBuffer = StringBuffer()

                if(it.isSuccessful) {
                    for(document in it.result!!) {
                        result.append(document.data.getValue("firstName")).append(" ")

                    }
                    nomUtilisateur.setText(result)
                }
            }
    }
    }

Fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".Flux.Flux">

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

</LinearLayout>

你能帮帮我吗? 谢谢!

E/RecyclerView:未连接适配器; skipping layout 适配器没有数据时可能会发生错误。 将 readFireStoreData() 函数移动到 return inflater 代码之前 如下所示。

override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    // Inflate the layout for this fragment

    readFireStoreData()

    return inflater.inflate(R.layout.fragment_page_profil, container, false)
}

您忘记添加布局管理器和方向了

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/rvPosts"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
    android:orientation="vertical" />

并且您还可以修复此函数,因为 return 该函数永远不会调用

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
 ): View? {

readFireStoreData()

return inflater.inflate(R.layout.fragment_page_profil, container, false)
}