适配器类Android中的片段到片段事务

适配器类Android中的片段到片段事务,android,kotlin,android-fragments,android-recyclerview,android-adapter,Android,Kotlin,Android Fragments,Android Recyclerview,Android Adapter,你好,我是Android开发的新手,我正在Android Studio中创建一个家庭应用程序。每当我想转到适配器类中的另一个片段时,该片段就是重叠的旧片段 我想从适配器类转到另一个片段名称SubFamilyFragment() 我在网上尝试了很多方法,但都没有成功 这是我的适配器类代码 import android.content.Context import android.view.LayoutInflater import android.view.View import android

你好,我是Android开发的新手,我正在Android Studio中创建一个家庭应用程序。每当我想转到适配器类中的另一个片段时,该片段就是重叠的旧片段

我想从适配器类转到另一个片段名称SubFamilyFragment()

我在网上尝试了很多方法,但都没有成功

这是我的适配器类代码


import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.CheckBox
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.RecyclerView
import com.example.mks.Fragments.SubFamilyFragment
import com.example.mks.Models.Family
import com.example.mks.R


class FamilyRecyclerAdapter(val context: Context, val familyList: ArrayList<Family>) : RecyclerView.Adapter<FamilyRecyclerAdapter.viewHolder>() {
    class viewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val familyName : TextView = view.findViewById(R.id.familyName)
        val familyMember : TextView = view.findViewById(R.id.familyMemberCount)
        val subFamily : CheckBox = view.findViewById(R.id.chkSubFamily)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): viewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.family_recycler_single_row,parent,false)

        return viewHolder(view)
    }

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

    override fun onBindViewHolder(holder: viewHolder, position: Int) {
        val number = familyList[position]
        holder.familyName.text = number.familyName
        holder.familyMember.text = number.familyMember.toString()
        holder.subFamily.text = number.subFamily.toString()

        holder.itemView.setOnClickListener {
            val activity : AppCompatActivity = it.context as AppCompatActivity

            if (number.subFamily){
//                val fragment2 = SubFamilyFragment()
//                val fragmentTransaction: FragmentTransaction = activity.supportFragmentManager.beginTransaction()
//                fragmentTransaction.replace(R.id.nav_host_fragment, fragment2)
//                fragmentTransaction.commit()

                val myFragment: Fragment = SubFamilyFragment()
                (it.getContext() as AppCompatActivity).supportFragmentManager.beginTransaction()
                    .replace(R.id.nav_host_fragment, myFragment).addToBackStack(null)
                    .commit()
            }
            else{
                Toast.makeText(context,"No Sub Family",Toast.LENGTH_SHORT).show()
            }
        }
    }
}```


This code is working but the fragment is overlapping.

Please tell me what to do.


导入android.content.Context
导入android.view.LayoutInflater
导入android.view.view
导入android.view.ViewGroup
导入android.widget.CheckBox
导入android.widget.TextView
导入android.widget.Toast
导入androidx.appcompat.app.appcompat活动
导入androidx.fragment.app.fragment
导入androidx.recyclerview.widget.recyclerview
导入com.example.mks.Fragments.SubFamilyFragment
导入com.example.mks.Models.Family
导入com.example.mks.R
类FamilyRecyclerAdapter(val-context:context,val-familyList:ArrayList):RecyclerView.Adapter(){
类viewHolder(视图:视图):RecyclerView.viewHolder(视图){
val familyName:TextView=view.findViewById(R.id.familyName)
val familyMember:TextView=view.findViewById(R.id.familyMemberCount)
val子族:CheckBox=view.findviewbyd(R.id.chkSubFamily)
}
override onCreateViewHolder(父级:ViewGroup,viewType:Int):viewHolder{
val view=LayoutInflater.from(parent.context).充气(R.layout.family\u recycler\u单行,parent,false)
返回视图保持器(视图)
}
重写getItemCount():Int{
返回familyList.size
}
覆盖BindViewHolder(holder:viewHolder,位置:Int){
val编号=家庭列表[位置]
holder.familyName.text=number.familyName
holder.familyMember.text=number.familyMember.toString()
holder.subFamily.text=number.subFamily.toString()
holder.itemView.setOnClickListener{
val活动:AppCompatActivity=it.context作为AppCompatActivity
if(数亚科){
//val fragment2=亚家族fragment()
//val fragmentTransaction:fragmentTransaction=activity.supportFragmentManager.beginTransaction()
//fragmentTransaction.replace(R.id.nav_主机_片段,fragment2)
//fragmentTransaction.commit()
val myFragment:Fragment=亚家族Fragment()
(作为AppCompatActivity的it.getContext()).supportFragmentManager.beginTransaction()
.replace(R.id.nav_host_fragment,myFragment)。addToBackStack(null)
.commit()
}
否则{
Toast.makeText(上下文,“无子族”,Toast.LENGTH\u SHORT.show())
}
}
}
}```
此代码正在工作,但片段重叠。
请告诉我该怎么办。

R.id.nav\u host\u片段
似乎暗示您正在使用导航架构组件和
NavHostFragment
。您尝试执行碎片事务而不是执行碎片事务有什么原因吗?
R.id.nav\u host\u碎片
似乎暗示您正在使用导航体系结构组件和
NavHostFragment
。有没有什么原因让你试着去做一些零碎的交易,而不是?