Search code examples
androidlistviewandroid-layoutandroid-listviewconvertview

Android: Should I use a ListView? (case: list elements with partially unknown structure)


Assumptions about the list (updated):

  • It will not contain more than 10 list elements (the list elements are defined by the xml layout below).
  • The height of every element is unknown, because the list element contain a LinearLayout that can have up to 20 child views (see xml below).

XML Layout of the list element:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation:"horizontal">

    <!--
        This LinearLayout is going to contain one or more 
        Views which will be added progammatically on runtime.
        The number of children views added to it depend on the 
        data to be displayed, and the only assumption that can 
        be made is that there's will be no more than 20 child 
        views for one particular instance of this LinearLayout.
    -->
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginRight="25dp"/>

    <ImageButton
        android:layout_width="25dip"
        android:layout_height="25dip"
        android:layout_gravity="center|top"
        android:layout_marginLeft="-25dp"/>
</LinearLayout>

Questions:

  • Does it make any sense to use a ListView for a layout that has such freedom in its structure (like the one above), and still being able to make use of the
    convertView as passed in to the ListView#getView(...)?
  • As an alternative, would it be wrong to put all the list elements in an outer
    LinearLayout and put this within a ScrollView? By doing this, I wouldn't get caching abilities of the ListView, but maybe it wouldn't be so heavy given the assumptions about the list? (see top). (Any pointers on how to make this alternative look and feel like a ListView? I'm thinking of applying standard colors and selectors etc.)

Solution

  • If you know some of your 10 elemets will be the same, you could use

    getItemViewType(int position)

    To be sure that the convertView will match your item type convertView