I’m helping a friend create an android app that will have screens with lists of info similar to a feed. I’ve been learning xml layout in Android and have some of the basics down, but don’t have a lot of familiarity with doing the java stuff. I’ve successfully created includes to seperate layout files for compontents within a screen, but what I’m wondering is if such a component can be used as a kind of template for feed/list items that get inserted programmatically on the back end. IE, is there a way to have Android create a list and for each list item it uses the external xml as a template? Sorry if this is somewhat vague, I’m new to this and trying to understand what our options are. TIA!
Share
Yes, every list item can be a custom layout. In fact you always have to define a layout for the list entries. You can either choose a prebuilt one from
android.R.layoutor you can use your own fromR.layout. You can specify it when you create the list adapter in code.Have a look at one of the
ArrayAdapterconstructors for example:The constructor takes a layout that will be used for the
ListViewchilds. Works similar with other adapters.What you usually do is inflating the layout inside
getView()of the adapter though. When you did that, fill all the data you need into the views of the layout, and return the view.Note that you get an argument called
convertView. This is one of the older layouts you already inflated before. In most cases the user just scrolled down and that entry is not visible anymore. If this convertView is not null, you can fill your data in there instead of inflating the whole layout again (thats expensive).You can find a working example inside the
ANDROID_SDK\samples\android-10\ApiDemos\src\com\example\android\apis\view\List5.javafile. Also take a look at the other list examples in that folder.