1

In m activity there is a fragment which contains several buttons. When one of them clicked those buttons suppose to vanish and other buttons suppose to appear. I could do it with a fragment per button group but it seems as an ugly solution since i will need many fragments when each of them holds just several buttons.

Could you suggest a better approach? I thought maybe creating several xml layout files and onClick inflating the necessary one in the same fragment but it doesnt seem to work. Is it the right approach? what could you suggest?

danidin
  • 371
  • 2
  • 17
  • What about using a layout that contains a set of buttons and gets its visibility GONE or VISIBLE? the other layout will get it's visibility VISIBLE or GONE, respectively. EASY. No need to assign different xml layouts. – Phantômaxx May 07 '14 at 08:06
  • Maybe im wrong but in my opinion in this case it would be kind of not clean solution since you will hold many button groups overlaping each other and it could be somewhat confusing for other programmers to read. I would like the code to be seperated and readable as much as possible. – danidin May 07 '14 at 08:08
  • This is an opinion. I think my solution is lighter and clean enough. But you might opt for the extreme separation. I respect your opinion – Phantômaxx May 07 '14 at 08:11
  • If you don't want to show / hide the content, you could just make an simple layout - just a container view - and several other xml layouts which contain the button groups. In the onCreateView method of the xml inflate the container view and after that just inflate the required the xml file in the container. This could be easier to read, not sure if this is gonna provide optimal performance, since inflating is not a fast operation - you could cache the views to solve this issue. – Samuil Yanovski May 07 '14 at 08:12
  • @BobMalooga Malooga -Thnx, but could you suggest how this separation can be achieved? Is using inflator each time a button clicked suppose to work? or are there other solutions? – danidin May 07 '14 at 08:13
  • You must inflate the new layout each time you want to use its contents. This introduces some overhead. – Phantômaxx May 07 '14 at 08:17
  • @BobMalooga - is it enough to just save the container recieved during onCreateView and after that onClick recieve the layoutInflator from getActivity and then inflate with the mentioned before container? or am i missing something? Also, are there other approaches that worth thinking about besides the two mentioned in our conversation? – danidin May 07 '14 at 08:20
  • This one seems promising: http://stackoverflow.com/a/18999694/2649012 – Phantômaxx May 07 '14 at 08:30
  • @BobMalooga thnx, i didnt understand what is the difference between ViewStub and just inflating and adding the inflated view to the container view...it is also seems that in the ViewStub you cannot remove the inflated view so once inflated you just can make it invisible/visible, or am i wrong? Anyway thnx for your help now i understand way better the alternatives for such scenario. – danidin May 07 '14 at 10:02

0 Answers0