Home > Enterprise >  How to switch different fragment onClicking ItemView of recyclerView item ? kotlin
How to switch different fragment onClicking ItemView of recyclerView item ? kotlin

Time:10-23

I want to switch the current fragment (containing recycler-View) to a different fragment on clicking the itemView of the recycler view.

Adapter Code

class pastActivity_adapter(options: FirestoreRecyclerOptions, val context: Context): FirestoreRecyclerAdapter<UserIssue, pastActivity_adapter.MyViewHolder>(options) {

class MyViewHolder(itemView : View) : RecyclerView.ViewHolder(itemView){
    val author_name : TextView = itemView.findViewById(R.id.post_author_name)
    val author_defect : TextView = itemView.findViewById(R.id.defect_post_text)
    val author_hostel : TextView = itemView.findViewById(R.id.author_hostel_no)
    
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
    val itemView = LayoutInflater.from(parent.context).inflate(R.layout.item_view_recycler,parent,false)
    return MyViewHolder(itemView)
}

override fun onBindViewHolder(holder: MyViewHolder, position: Int, model: UserIssue) {
    val db = FirebaseFirestore.getInstance()
    val userId = FirebaseAuth.getInstance().currentUser?.uid
    val userRef = db.collection("Users")
    holder.author_defect.text = model.issueDefect
    holder.author_hostel.text = "Hostel no. ${model.userHostelno}"
    holder.dateTime.text = model.issueDataTime
    holder.typeIssue.text = "IssueType - ${model.issueType}"
    if (model.admin_resolved_checkbox){
        holder.processStatus.setImageDrawable(ContextCompat.getDrawable(context,R.drawable.greentick_icon))
    }

    **holder.itemView.setOnClickListener{
        FragmentActivity.beginTransaction()
            .replace(R.id.view_complaint_fragment, view_allComplaint())
            .commit()
       }
    }**

}

XML File

<FrameLayout
    android:id="@ id/view_complaint_fragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"></FrameLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

CodePudding user response:

(anyViewContext as? Activity)?.supportFragmentManager.beginTransaction().replace(containorId, Fragment()).commitAllowingStateLoss()

that should do it

CodePudding user response:

In my case this works

holder.itemView.setOnClickListener {
        val transaction = (context as pastRegistrationActivity).supportFragmentManager.beginTransaction()
        val fragmentwo = detailComplaintView()
        transaction.replace(R.id.view_complaint_fragment,fragmentwo)
        transaction.addToBackStack(null)
        transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE)
        transaction.commit()
    }
  • Related