Home > Blockchain >  How to reuse a layout in other fragments
How to reuse a layout in other fragments

Time:09-17

I'm using a Navigation Drawer Activity. When I made two fragments, I noticed that their layout was the same. The difference is that pressing the button executes a different method for each fragment. But the other buttons do the same thing on the two fragments. How do I reuse the layout and java code for the fragments and be able to specify a method to be executed on each fragment, knowing that it will need View access?

CodePudding user response:

One way to do this is to create two different fragment classes that load the same XML layout and assign the click handler for the button programmatically.

CodePudding user response:

you can use visibility of the button.use same xml for both fragment instead of one button use two, then in fragment check which fragment is in view then change the visibility.Gone of the not needed button for the activity. if you are using databindng then do different task for two button in viewmodel that will do the rest of your button task

CodePudding user response:

You can set a Boolean (fals is the first "fragment", true the second). Then you can use a check in witch "fragment" you are and execute the code.

if(true){
executeFirstButtonUse()
}else{
executeSecondButtonUse()
}

Then U wil have 1 fragmen, 1 view, (1 viewModel).

I u coud provide more info or code I could provide more detail or an other method.

  • Related