Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inflating Layout XML to custom LinearLayout creates redundant LinearLayout view

I'm creating a custom LinearLayout component. It's designed to mimic some functionality of a ListView, for use in a ScrollView, since I can't/shouldn't use a ListView inside a ScrollView. Let's call it CustomListView. Furthermore, I'm creating another custom LinearLayout to represent each item in this pseudo-listview, let's call it NewItemView.

Anyway, when initialising NewItemView, I'm setting it up by inflating a layout XML. The layout XML declares a LinearLayout as its root view, and is not dissimilar to layouts like simple_list_item_multiple_choice.xml.

Overall, what happens is that the main activity has a CustomListView. We call addItem(Item item) on CustomListView to add a new item to the list, which in reality, creates a new NewItemView, and populates it with data from the Item class.

I'm inflating in newListItem.initListItem(Context context) as so:

 ((Activity)getContext())
            .getLayoutInflater()
            .inflate(R.layout.list_item, this, true);

This actually works! However, the issue I have is that on inspection with Monitor, there's a redundant LinearLayout just sitting around each item in the list. My understanding is that this is generally a bad idea. There's the LinearLayout for CustomListView, then for each item, there's a LinearLayout, with just one child; a LinearLayout! That last one contains the actual children.

I expect that it has something to do with the fact that I'm creating a custom component based off LinearLayout, and then inflating a layout with a LinearLayout at it's root.

like image 562
user2521989 Avatar asked Jun 25 '13 23:06

user2521989


1 Answers

Yes, this is what the merge tag is for. Simply replace your root LinearLayout tag with merge, and that should do what you need.

See this blog post.

like image 52
Kevin Coppock Avatar answered Oct 23 '22 20:10

Kevin Coppock