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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With