How to use Jetpack View Binding for Abstract Fragments

Viewed 61

I have two fragments which have a lot in common. Therefore, I have an abstract fragment with an "abstract" layout file for this, which is included in the other two layout files.

However, I am not sure, how to use Jetpack View Binding for the abstract fragment, because I don't know which layout file is used.


Here a simplified example of my problem:

Abstract fragment (ExampleAbstractFragment.kt):

abstract class ExampleAbstractFragment : Fragment() {
    
    override fun doSomething() {
        val abstractTextView = findViewById<TextView>(R.id.abstract_text_view)
        // ^^ How to get this with View Binding
        val abstractButton = findViewById<Button>(R.id.abstract_button)
        // ^^ How to get this with View Binding
    }
}

Layout file of abstract fragment (fragment_example_abstract.xml):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/abstract_text_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/test" />

    <Button
        android:id="@+id/abstract_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/test" />
</LinearLayout>

--

First fragment (ExampleFragmentA.kt):

class ExampleFragmentA : ExampleAbstractFragment() {

    private var _binding: FragmentAExampleBinding? = null
    private val binding get() = _binding!!

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        _binding = FragmentAExampleBinding?.inflate(inflater, container, false)
        return binding.root
    }

    override fun onDestroyView() {
         super.onDestroyView()
         _binding = null
    }

    override fun doSomething() {
        val textView = binding.fragmentAExampleTextView
        val button = binding.fragmentAExampleButton
    }
}

Layout file of first fragment (fragment_a_example.xml):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/fragment_a_example_text_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/test" />

    <include
        layout="@layout/fragment_example_abstract"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/fragment_a_example_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/test" />
</LinearLayout>

--

Second fragment (ExampleFragmentB.kt):

class ExampleFragmentB : ExampleAbstractFragment() {

    private var _binding: FragmentBExampleBinding? = null
    private val binding get() = _binding!!

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        _binding = FragmentBExampleBinding?.inflate(inflater, container, false)
        return binding.root
    }

    override fun onDestroyView() {
         super.onDestroyView()
         _binding = null
    }

    override fun doSomething() {
        val editText = binding.fragmentBExampleEditText
        val imageView = binding.fragmentBExampleImageView
    }
}

Layout file of second fragment (fragment_b_example.xml):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <EditText
        android:id="@+id/fragment_b_example_edit_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/test" />

    <include
        layout="@layout/fragment_example_abstract"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <ImageView
        android:id="@+id/fragment_b_image_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/test" />
</LinearLayout>
0 Answers
Related