2

I have two ListViews above each other:

  • When the second ListView is empty, the first ListView may fill up the whole area.
  • When the first ListView is empty the second ListView may fill up the whole area.
  • When both ListViews have items, the first ListView must push down the second ListView, but at least 60dp of the second ListView must be kept visible. The first ListView must not be higher than its contents.

I almost solved all these requirements with this layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  >
  <ListView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:divider="@null"
    android:cacheColorHint="@android:color/transparent"
    />
  <ListView
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:minHeight="60dp"
    android:divider="@null"
    android:cacheColorHint="@android:color/transparent"
    />
</LinearLayout>

When the second ListView is empty, I set the Visibility to Gone programmatically to meet the first requirement.

But when the first ListView fills up, it will push down the second ListView until it disappears.

I tried to solve this by changing the layout_height programmatically from a custom adaper when the first ListView grows too large. But my list items have different sizes, so after changing the height to a fixed value I don't know how to detect the content height when items are added or changed.

Does anybody has an idea how to solve this?

Marcel Wolterbeek
  • 3,367
  • 36
  • 48
  • so there are two posiblities 1) show listview1 and hide listview2 2)show listview1 about 80% of screen height and show listview 2 about 20% of screen height??/ – NaserShaikh May 28 '13 at 07:56
  • you can set height and visibility pragmatically.. just check for second listview's data source if its empty then setvisiblity gone to listview 2 and fill parent to list one – NaserShaikh May 28 '13 at 07:58
  • No @Nasser, the first ListView must never be larger than its content. So when the first ListView fills 10% of the screen height, the second ListView must fill 90% of the screen height, etc. – Marcel Wolterbeek May 28 '13 at 08:03
  • are you using custom adapter for both list views? if yes then you can simply take height of your listItem and multiply it by number of rows in listView to set Height of your first ListView. – NaserShaikh May 28 '13 at 08:09
  • @Nasser thats not feasible.The rows may be many and multiplying the height would make it difficult to scroll the `ListView` – Nezam May 28 '13 at 08:25
  • I guess I will end up with the calculation in the custom adapter. Its not that simple, because the items are of different size and when an item is selected it gets bigger. Also, I would like to implement a reusable solution which is not limited to ListViews. I did that for iOS, which was quite hard to do and I kept telling "hey, this will be easy in Android!", but it seems not... – Marcel Wolterbeek May 28 '13 at 08:25
  • or you can set ListViews heght by taking phones screen Heght and give some % for first listview and some for second. – NaserShaikh May 28 '13 at 09:03
  • try something like the answer in this related question - it looks promising http://stackoverflow.com/questions/5487552/limit-height-of-listview-on-android – Richard Le Mesurier Jun 18 '14 at 10:23

0 Answers0