Not 100% on terminology so bear with me.
I have an activity that compares two items side by side, one on the left and another on the right. The items contain the same layout so I was wondering if I could reuse the left fragment java and XML file and instantiate a new instance. It is possible that I just copy and paste the left fragment files into identical files for right side but I feel as though there must be a more elegant method.
tdlr: Is there a way to make two or more instances of the same fragment operating in the same layout/activity?
You need to make a parent layout with 2 side-by-side containers (framelayout, for example or fragments directly).
If you prefer in code, then add the fragments through FragmentManager transation in those containers.
getSupportFragmentManager()
.beginTransaction().add(R.id.left_container,new YourFragment(),"some tag1").commit();
getSupportFragmentManager()
.beginTransaction().add(R.id.right_container,new YourFragment(),"some tag2").commit();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With