Android layout efficiency, multiple views or single with hidden views?

I have a ListView with layouts in them. Sometimes layouts must look one way, and sometimes the other. Basically this is text laid out in different areas with different weights, etc. The ratio of the main species in comparison with other species is about 5: 1. When it needs to be changed, only 2/3 of the whole representation changes.

I think I have two options:

  • (so it is now). One layout (therefore, it never needs to be re-pumped, since the processed views are all the same), and the second 2/3 of the view is hidden until it is needed, which will be changed, then the code will open it and hide the original 2/3 of the view.

  • (differently) Two layouts, with 1/3 double layout, and each of which has its own 2/3. Then, when you need to use one of the different layouts, the old view from the ListView utility is discarded, and the new correct view gets pumped back and forth when moving the list.

So what I'm asking here is that it’s better to hide / show parts of the same layout to prevent the ListView from re-inflating more layouts or to have two layouts without unnecessary hidden views, and accumulate them when they are needed?

+4
source share
2 answers

If TextViews is the main part of these layouts, I don’t think there will be a big difference in performance so you can go anyway. I will go with option two because:

  • The ratio between the number of lines of regular lines and special lines is great. If you adhere to a unified approach to placement, you will have to change the layouts in the code for each line, regular or special, because you can deal with a redesigned View that has 2/3 of the parts hidden when it is time to show normal (so you're in end up changing the layout every time). If you used the second option, you could eliminate this part from the getView method and just use switch to see what type of row you are dealing with (without additionally modifying the layout of the row every time in getView , you will enable the ListView descriptor by providing you with the correct line layout type). Do not worry about bloating another layout file (once), this does not affect performance.

  • Using one type of layout means that you have Views left in memory even if the user does not see them (so you can prevent the ListView inflating more Views (only once, actually), but at the same time you take up more memory with those that you don’t even show to the user). The first option has the advantage that the data binding to the string is simpler, since you have all the views at your disposal (even if the user does not see part of them) instead of the second option, where you would see what data should be displayed based on the type strings.

  • Reading code. Having two types of lines and testing to find out which one you are using in the getView method, it is more getView , because you see what logic you have for each type of line. It will also be better if you plan to change these line layouts in the future or add more line types.

ListView does a great job of viewing views if you help it provide row types. Edit: ListView save a redesigned view for each type declared in the adapter. Thus, the ListView will inflate the second layout only the first time it finds it in the ListView , the next time it needs a string with a type, it will use the recycled one (and will support 2 types of recycled views, the type of the first and second row) .

+2
source

Actually this is a bit of an additional GC IMHO should not be a driver for your solution.

I would take a route that is easier to maintain and understand.

There is also a nice useful include tag in xml, for example:

 <include android:id="@+id/abc" android:layout_height="wrap_content" android:layout_width="fill_parent" layout="@layout/abc" , where abc = the xml file you want to include /> 
0
source

Source: https://habr.com/ru/post/1411681/


All Articles