0

I want to navigate to DetailsFragment onClick of the list item (book_item.xml)

book_item.xml

<LinearLayout 
        ...
        onBooksClickListener="@{result}">
... 
</LinearLayout>

Now it works fine for Fragment (Full Screen)

I want to make it overlay fragment so want to convert it to Dialog Fragment

Working Code For Fragment

Fragment Class

class DetailsFragment : Fragment() {
 private val args by navArgs<DetailsFragmentArgs>()
 override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?): View? {
    val view = inflater.inflate(R.layout.fragment_details, container, false)
    }
 }

RowBinding Class

class BooksRowBinding  {
    companion object {

        @ExperimentalCoroutinesApi
        @BindingAdapter("onBooksClickListener")
        @JvmStatic
        fun onBooksClickListener(linearLayout: LinearLayout, result: Result){
            linearLayout.setOnClickListener{
                try {
                    val action = BooksListFragmentDirections.actionBooksListFragmentToDetailsFragment(result)
                    linearLayout.findNavController().navigate(action)

                    }catch (e: Exception){
                    Log.d("OnBooksClickListener",e.toString())
                }
            }
        }

To convert it to DailogFragment I tried using the google android docs but it didnt't explain how to open dialogFragment from outside the Fragment class

Broken Code of DialogFragment

Fragment Class

class DetailsFragment : DailogFragment() {
 private val args by navArgs<DetailsFragmentArgs>()
 override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?): View? {
    val view = inflater.inflate(R.layout.fragment_details, container, false)
    }
 }

 

RowBinding Class

class BooksRowBinding  {
    companion object {

        @ExperimentalCoroutinesApi
        @BindingAdapter("onBooksClickListener")
        @JvmStatic
        fun onBooksClickListener(linearLayout: LinearLayout, result: Result){
            linearLayout.setOnClickListener{
                try {
                    DetailsFragment().show(childFragmentManager,DetailsFragment.TAG) 
                   /****************ERROR *******************
                    this works only if you call this from within the 
                    onCreateMethod of the Fragment
                   ******************************************/
                    }catch (e: Exception){
                    Log.d("OnBooksClickListener",e.toString())
                }
            }
        }

Now i am Getting error: Unresolved reference: childFragmentManager

my_nav.xml

<navigation 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"
    android:id="@+id/my_nav"
    app:startDestination="@id/booksListFragment">
    <fragment
        android:id="@+id/booksListFragment"
        android:name="we.are.suvikranth.ui.BooksListFragment"
        android:label="Suvikranth"
        tools:layout="@layout/fragment_books_list">
        
        <action
         android:id="@+id/action_booksListFragment_to_detailsFragment"
            app:destination="@id/detailsFragment" />
    </fragment>
    <fragment 
        android:id="@+id/detailsFragment"
        android:name="we.are.suvikranth.ui.DetailsFragment"
        android:label="Books Details"
        tools:layout="@layout/fragment_details" >
        <argument
            android:name="result"
            app:argType="we.are.suvikranth.models.Result" />
    </fragment>
</navigation>
krishnaacharyaa
  • 14,953
  • 4
  • 49
  • 88
  • 1
    You should not be directly creating a DialogFragment yourself if you are using Navigation. Is there a reason you're not using a [dialog destination](https://developer.android.com/guide/navigation/navigation-create-destinations#create-dialog)? – ianhanniballake Dec 18 '21 at 22:00
  • @ianhanniballake Thanks for Replying, Because it wasn't given in the google docs https://developer.android.com/guide/fragments/dialogs – krishnaacharyaa Dec 19 '21 at 02:38
  • @ianhanniballake Thanks to you . I had used navigation graph (dialog destination) prior while doing the full fragment. I reused that and changed the nav.xml Fragment component to dialog component – krishnaacharyaa Dec 19 '21 at 02:57

1 Answers1

0

Use the previous code to navigate to dialog Fragment

But change the fragment to dialog in my_nav.xml

Now the RowBinding Class looks like

class BooksRowBinding  {
    companion object {

        @ExperimentalCoroutinesApi
        @BindingAdapter("onBooksClickListener")
        @JvmStatic
        fun onBooksClickListener(linearLayout: LinearLayout, result: Result){
            linearLayout.setOnClickListener{
                try {
                    val action = BooksListFragmentDirections.actionBooksListFragmentToDetailsFragment(result)
                    linearLayout.findNavController().navigate(action)

                    }catch (e: Exception){
                    Log.d("OnBooksClickListener",e.toString())
                }
            }
        }

my_nav.xml looks like

<navigation 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"
    android:id="@+id/my_nav"
    app:startDestination="@id/booksListFragment">
    <fragment
        android:id="@+id/booksListFragment"
        android:name="we.are.suvikranth.ui.BooksListFragment"
        android:label="Suvikranth"
        tools:layout="@layout/fragment_books_list">
        
        <action
         android:id="@+id/action_booksListFragment_to_detailsFragment"
            app:destination="@id/detailsFragment" />
    </fragment>
    <dialog
        android:id="@+id/detailsFragment"
        android:name="we.are.suvikranth.ui.DetailsFragment"
        android:label="Books Details"
        tools:layout="@layout/fragment_details" >
        <argument
            android:name="result"
            app:argType="we.are.suvikranth.models.Result" />
    </dialog>
</navigation>
krishnaacharyaa
  • 14,953
  • 4
  • 49
  • 88