Rur
Rur

Reputation: 1

How to return multiple values VIEW in Kotlin?

I was trying to change ImageView in one Fragment from another Fragment. But since this cannot be done without container, I have to create another view value with the inflater method. Then I need to return these view values with the inflater methods. How do I return two views?

code is written in a fragment:

override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {

        val view = inflater.inflate(R.layout.firstfragment, container, false)
        val view2 = inflater.inflate(R.layout.secondfragment, container, false)

        val imgPF: ImageView = view2.findViewById(R.id.imageView)

        imgPF.setImageResource(R.drawable.image_29)

        view.ButtonFromFirstFragment.setOnClickListener{
            //Unimportant code
        }

        setHasOptionsMenu(true)

        return view //Approximately here i need to return two views, but how? 
                    //And can it be done somehow differently?
    }

Upvotes: 0

Views: 747

Answers (1)

Basically you should not have 2 fragments within a fragment. Please double check that you are not confusing fragments with views.

If you want to have those 2 fragments in an activity and you are showing both of your fragments at the same time then you should have 2 separate fragments:

Fragment A:

(...)

override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {

        val view = inflater.inflate(R.layout.firstfragment, container, false)

        view.ButtonFromFirstFragment.setOnClickListener{
            //Unimportant code
        }

        return view
    }

(...)

Fragment B: (...)

override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {

        val view = inflater.inflate(R.layout.secondfragment, container, false)

        val imgPF: ImageView = view.findViewById(R.id.imageView)

        imgPF.setImageResource(R.drawable.image_29)
    
        return view 
    }

Upvotes: 1

Related Questions