找不到在片段中插入 RecyclerView 的方法(Kotlin)

cant find a way to insert RecycleView inside fragment (Kotlin)

我是 Kotlin 的新手,我尝试创建一个购物清单应用程序,但我遇到了片段内 RecycleView 的问题。 在该片段中,我有一个按钮,用户单击该按钮并将 Item 添加到列表中,但是当我尝试在 OnCreateView 函数中添加布局管理器时出现错误。 我试过这个:

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

        val view = inflater.inflate(R.layout.fragment_shopping_list, container, false)


        rv_shoppinglist.layoutManager = LinearLayoutManager(context) \ this is where i get ERR

        return view
    }

我得到了一个错误 -> java.lang.IllegalStateException: rv_shoppinglist must not be null

所以我添加了这一行 rv_shoppinglist.layoutManager = LinearLayoutManager(context) 在不同的功能上

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        rv_shoppinglist.layoutManager = LinearLayoutManager(activity)
    }

现在该应用程序可以运行,当我将项目添加到列表时,它确实会在 RecycleView 上显示这些项目,但不是显示它们,而是用下划线显示它们,而是用屏幕尺寸间隙显示它们 图片:

编辑: 这是 RV_CHILD 的 XML 文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/tv_item_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</RelativeLayout>

这是 shopping_fragment

的 XML 文件
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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=".fragments.shopping_list">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

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

        <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:id="@+id/fab_shoppiglist"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:src="@drawable/ic_plus"
            android:layout_margin="16dp" />


    </RelativeLayout>

</FrameLayout>

正如一位评论者所指出的,您的 viewholder 布局高度(在您的情况下为 RV_CHILD)似乎设置为 match_parent,而实际上应该是 wrap_content

您需要将布局的相对高度设置为wrap_content,否则会占满整个屏幕

<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    
        <TextView
            android:id="@+id/tv_item_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </RelativeLayout>