单击 RecyclerView 项目时调用片段并在同一活动中显示它们

amn*_*non 6 java android android-fragments android-recyclerview

由于我只从按钮更改为 RecyclerView (并且它与按钮一起工作得很好),我知道我的 java 代码工作正常,所以现在我面临一个问题,因为单击 RecyclerView 中的卡片会使用 FragmentManager 调用片段,但不会显示它。这是我的 Activity.xml 代码。

<线性布局
    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"
    安卓:layout_width =“match_parent”
    安卓:layout_height =“match_parent”
    机器人:方向=“垂直”>
    <androidx.constraintlayout.widget.ConstraintLayout
        安卓:layout_width =“match_parent”
        安卓:layout_height =“match_parent”
        工具:context=".mainSearch"
        android:background="#f2f2f2">

        <androidx.recyclerview.widget.RecyclerView
            安卓:layout_width =“match_parent”
            安卓:layout_height =“match_parent”
            android:id="@+id/recyclerView"
            android:orientation="水平" />

    </androidx.constraintlayout.widget.ConstraintLayout>
    <片段
        android:name="com.diamcom.blue.StoneCodeFragment"
        android:id="@+id/fragment_place"
        安卓:layout_width =“match_parent”
        机器人:layout_height =“match_parent”>
    </片段>
</线性布局>

我想知道我做错了什么?

小智 2

我认为你在这里遇到的问题是将 recyclerview 和片段都作为宽度和高度的 match_parent 。我认为你可以做到这一点的一种方法是包装容器视图,该容器视图将回收器视图和片段保存在框架布局中,并在单击回收器视图中的任何卡片时使容器视图消失。