Android: Just Show This, Instead of Don't Show Everything Else
I have many different items on my main.xml layout (30+). I have to constantly throughout an activity I have to use...
tv1.setVisibility(View.GONE);
tv2.setVisibility(View.GONE);
...for at least 20 of the items. I think it would be easier if some how you could make it so, instead of View.GONE, you can choose what items to show and hid开发者_JAVA百科e the rest.
tv3.setVisibility(View.SHOW);
tv4.setVisibility(View.GONE);
// Some code to hide the rest
Is there anything I can do to make this similar, or maybe some code that just hides the rest?
Instead of putting everything into one layout and changing the visibility, is it possible you could make multiple layouts and switch between them? that becomes just one line to switch, and a few more to initialize the settings for the components.
I like the ideas of layouts suggested by mtmurdock,
I would also suggest to automate this setVisibility process.
you could do something along the lines of
1) onCreate: store all your tvX in a list
2) onChangeVisibility(View tAppear){
for(View v : viewList){
if(v == tAppear){
v.setVisibility(View.SHOW);
}else{
v.setVisibility(View.GONE);
}
or if you have several view to switch you car do a (View []tAppear)
精彩评论