Splitting an XML layout into a custom LinearLayout creates a redundant LinearLayout view

I am creating a custom component of LinearLayout . It is intended to simulate some ListView functions for use in ScrollView , since I cannot / should not use / ListView inside a ScrollView . Let me call it CustomListView. In addition, I create another custom LinearLayout to represent each item in this pseudo-list, let it call NewItemView.

Anyway, when initializing NewItemView, I customize it by inflating the XML format. The XML format declares LinearLayout as its root appearance and is no different from layouts such as simple_list_item_multiple_choice.xml .

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

I inflate newListItem.initListItem(Context context) like this:

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

It really works! However, the problem is that when checking with the monitor there is an excess LinearLayout just sitting around each item in the list. I understand that this is generally a bad idea. There LinearLayout for CustomListView, then for each element there is LinearLayout , with one child; a LinearLayout ! This last one contains actual children.

I expect this to be because I am creating a custom component based on LinearLayout and then inflating the layout with LinearLayout on it root.

+7
source share
1 answer

Yes, this is a merge tag. Just replace your LinearLayout root tag with merge and it should do what you need.

See this blog post .

+5
source

All Articles