Java – use the same fragment multiple times in the same activity / layout

Not a 100% term, so please be patient

I have an activity to compare two items side by side, one on the left and the other on the right These projects contain the same layout, so I wonder if I can reuse the left fragment Java and XML files and instantiate a new instance I may just copy and paste the fragment file on the left into the same file on the right, but I think there must be a more elegant method

Tdlr: is there any way to make two or more instances of the same fragment run in the same layout / activity?

Solution

You need to create a parent layout using 2 side - by - side containers, such as FrameLayout or direct clips

If you prefer code, add fragments to these containers through the fragment manager transformation

getSupportFragmentManager()
     .beginTransaction().add(R.id.left_container,new YourFragment(),"some tag1").commit();

    getSupportFragmentManager()
     .beginTransaction().add(R.id.right_container,"some tag2").commit();
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>