【问题标题】:cant find a way to insert RecycleView inside fragment (Kotlin)找不到在片段中插入 RecyclerView 的方法(Kotlin)
【发布时间】:2020-09-01 13:41:29
【问题描述】:

我是 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
    }

我得到了一个 ERR -> 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>

【问题讨论】:

  • 请为您用来表示您的 RecyclerViews 列表项的布局文件添加 xml
  • 也许您的 ViewHolder 布局的高度是“match_parent”而不是“wrap_content”?
  • 请为recycler view和recycler view items添加你的xml布局文件。

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


【解决方案1】:

您需要将相对布局高度设置为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>

【讨论】:

    【解决方案2】:

    正如评论者指出的那样,您的视图布局高度(在您的情况下为 RV_CHILD)似乎设置为 match_parent,而它可能应该是 wrap_content

    【讨论】:

      猜你喜欢
      • 2021-06-29
      • 1970-01-01
      • 1970-01-01
      • 2021-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-05
      • 2021-05-15
      相关资源
      最近更新 更多