I have a scrollview in an activity that I want to populate. Each item I want to add to the list is made up of several views (text view, image view, etc). Instead of programmatically creating each view, and adding it to a linear layout, and then adding that to the containing layout, is it possible to instead create a predefined layout resource with these items and instead add it to the view and programmatically change the item contents?
Essentially, is it possible to do something like:
container.addView(R.layout.listItem);
And if so, how could I access views within the list item to change them?
It's just what inflate(int, ViewGroup, boolean) does with the third parameter attachToRoot
set to true
getLayoutInflater().inflate(R.layout.listItem, container, true);
After that everything is straightforward
container.findViewById(R.id.text_view)
You have to "inflate" your view by LayoutInflater . For example like this:
View view = LayoutInflater.from(context).inflate(R.layout.listItem, container, true);
or View view = LayoutInflater.from(context).inflate(R.layout.listItem, container, false); container.addView(view);
View view = LayoutInflater.from(context).inflate(R.layout.listItem, container, false); container.addView(view);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.