如何在我的片段中用适配器数据填充我的回收视图?

How to fill my recyclerview with adapterdata in my fragment piece?

所以我已经在互联网上搜索了一段时间,但我似乎找不到正确的主题来帮助我解决这个问题。 我有以下与这个问题相关的代码:

这是我的适配器class。

class SmoelenBoekAdapter(var profiles: Array<Profile>) : RecyclerView.Adapter<CustomViewHolder>(){

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CustomViewHolder {
        val layoutInflater = LayoutInflater.from(parent.context)
        val layout = layoutInflater.inflate(com.otten.nvvofrankversie.R.layout.recyclerview_smoelenboek, parent, false)
        return CustomViewHolder(layout)
    }

    override fun onBindViewHolder(holder: CustomViewHolder, position: Int) {
        val positioner = profiles.get(position)
        var profileImage = positioner.profile_image
        var firstName = positioner.first_name
        var lastName = positioner.last_name
        var plaats = positioner.place
        var adres = positioner.address
        holder.view.naamSmoelenBoek.text = firstName.string + " " + lastName.string
    }

    override fun getItemCount(): Int {
        return profiles.size
    }
}

class CustomViewHolder(val view: View) : RecyclerView.ViewHolder(view)

这是我的'MainActivity'(对于这个特定片段)

class SmoelenBoek : ApplicationFragment(){
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.fragment_smoelenboek, null)
    }

    /*override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        smoelenboekRecyclerView.adapter = SmoelenBoekAdapter(profiles)
    }*/
}

xml 其中adapterinfo必须填写:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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="wrap_content"
    android:orientation="vertical">


    <ImageView
        android:id="@+id/profielPicSmoelenBoek"
        android:layout_width="100dp"
        android:layout_height="100dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/business_icon" />

    <TextView
        android:id="@+id/naamSmoelenBoek"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="10dp"
        android:text="Frank Otten"
        android:textColor="@color/colorPrimaryDark"
        android:textSize="18sp"
        app:layout_constraintBottom_toTopOf="@+id/profielPicSmoelenBoek"
        app:layout_constraintStart_toEndOf="@+id/profielPicSmoelenBoek"
        app:layout_constraintTop_toBottomOf="@+id/profielPicSmoelenBoek" />

    <TextView
        android:id="@+id/textView17"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="15dp"
        android:text=">"
        android:textSize="24sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="@+id/naamSmoelenBoek" />
</androidx.constraintlayout.widget.ConstraintLayout>

这是我有一个 recyclerview 的片段,需要用上面的内容填充 xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/imageView8"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/offer_bg"
        android:adjustViewBounds="true"
        android:scaleType="center"/>

    <Button
        android:id="@+id/searchInSmoelenBoek"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="15dp"
        android:layout_marginTop="60dp"
        android:layout_marginEnd="15dp"
        android:background="@color/lightGray"
        android:hint="Zoek in smoelenboek"
        android:padding="10dp"
        android:textAlignment="textStart"
        android:textColor="@color/colorAccent"
        android:textStyle="bold"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView11"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="15dp"
        android:layout_marginTop="60dp"
        android:text="Vind gemakkelijk alle\naangesloten orthodontisten"
        android:textColor="@color/white"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/searchInSmoelenBoek" />

    <TextView
        android:id="@+id/textView15"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="15dp"
        android:layout_marginEnd="15dp"
        android:text="Aangesloten orthodontisten"
        android:textAlignment="textStart"
        android:textColor="@color/browser_actions_title_color"
        android:textSize="18sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/imageView8" />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/smoelenboekRecyclerView"
        android:layout_width="409dp"
        android:layout_height="440dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView15"/>
</androidx.constraintlayout.widget.ConstraintLayout>

我在这里做错了什么或遗漏了什么? 我想我仍然需要正确设置适配器,但我也无法弄清楚...... 希望有人能帮助我!

// Set layout manager for recycler view
smoelenboekRecyclerView.layoutManager = LinearLayoutManager(this);
// Set adapter to recycler view
smoelenboekRecyclerView.adapter = SmoelenBoekAdapter(profiles)   


class CustomViewHolder(val view: View) : RecyclerView.ViewHolder(view){
    val tvNaamSmolText = view.naamSmoelenBoek
}

override fun onBindViewHolder(holder: CustomViewHolder, position: Int) {
        val positioner = profiles.get(position)
        var profileImage = positioner.profile_image
        var firstName = positioner.first_name
        var lastName = positioner.last_name
        var plaats = positioner.place
        var adres = positioner.address
        holder.tvNaamSmolText.text = firstName.string + " " + lastName.string
    }

试试这个

class SmoelenBoek : ApplicationFragment(){
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        View view =  inflater.inflate(R.layout.fragment_smoelenboek, null)
view.smoelenboekRecyclerView.layoutManager = LinearLayoutManager(this);
view.smoelenboekRecyclerView.adapter = SmoelenBoekAdapter(profiles)
return view
    }
    }


}
class SmoelenBoek : ApplicationFragment(){
    var profiles: Array<Profile> = arrayOf()
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.fragment_smoelenboek, null)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        SharedInstance.api.getAllUsers {
            profiles = it
            smoelenboekRecyclerView.layoutManager = LinearLayoutManager(context)
            smoelenboekRecyclerView.adapter = SmoelenBoekAdapter(profiles)
        }
    }
}

这是我的问题的解决方案。 我不得不在异步调用之后将布局管理器和适配器放在 OnviewCreated 中(我忘记实现了,所以这是问题的一部分),以便可以设置布局管理器并且适配器可以用项目填充我的回收视图。 无论如何,谢谢你们的帮助!