1

I have a ConstraintLayout where I have three different ListView's. The content of these ListViews is set dynamically. So I don't really know how many items they will get on runtime.

I want the layout as follows: The first ListView should start at the top of the screen and the second should be almost right under it. (just with some fix distance). The third one should stick to the bottom of the screen, if the first two ListViews aren't taking to much space already. If they do and so they would interfere with the third ListView on the bottom, it should put below the second ListView with some fix distance.

How could I achieve this? At the moment I implemented it so that the third ListView sticks to the bottom. But it will be overwritten from the second ListView if it has too many items, or the screen of the phone is too small.

Here my Layout:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/backgroundColor"
    android:fitsSystemWindows="true"
    tools:context=".activities.fragments.PreferenceCategoryFragment">

    <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp"
        android:layout_marginTop="10dp"
        android:textAppearance="@style/introductionTitle"
        android:text="Allgemeines"
        app:layout_constraintTop_toTopOf="parent"/>

    <View
        android:id="@+id/divider1"
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:background="@color/darkGray"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp"
        app:layout_constraintTop_toBottomOf="@id/title"/>

    <ListView
        android:id="@+id/settingsCategories"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:divider="@null"
        app:layout_constraintTop_toBottomOf="@id/divider1">
    </ListView>

    <TextView
        android:id="@+id/title2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp"
        android:layout_marginTop="30dp"
        android:textAppearance="@style/introductionTitle"
        android:text="Sonstiges"
        app:layout_constraintTop_toBottomOf="@id/settingsCategories"/>

    <View
        android:id="@+id/divider2"
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:background="@color/darkGray"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp"
        app:layout_constraintTop_toBottomOf="@id/title2"/>

    <ListView
        android:id="@+id/settingsCategoriesLegal"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:divider="@null"
        app:layout_constraintTop_toBottomOf="@id/divider2">
    </ListView>

    <ListView
        android:id="@+id/logout"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:divider="@null"
        android:layout_marginTop="50dp"
        app:layout_constraintBottom_toBottomOf="parent">
    </ListView>

</android.support.constraint.ConstraintLayout>

For better understanding see this image of how it looks like on a big screen phone: enter image description here

And this is how it looks like if the screen is too small:

enter image description here

progNewbie
  • 4,362
  • 9
  • 48
  • 107
  • 4
    You actually only want to use one "ListView" which displays all items. Also you dont really want to use ListView anyway, RecyclerView is the way to go. So my suggestion is reading up on RecyclerView and using the ViewHolder pattern with different View Types to achieve your goal. Using only one Recyclerview will enable proper scrolling too, so nothing gets pushed out – StarterPack Aug 20 '18 at 14:32
  • As @StarterPack said I think using a RecyclerView is the best option. This https://stackoverflow.com/questions/26530685/is-there-an-addheaderview-equivalent-for-recyclerview post explains how to inflate different layouts for the headers you have. – Aaron Thompson Aug 20 '18 at 14:53
  • @StarterPack thanks for this hint :) I will have a look at it. – progNewbie Aug 20 '18 at 16:38
  • @StarterPack I rewrote my code and have everything in a `RecyclerView` now. It scrolls like a charm on small Screen. But I am not sure how I get the previous last listview on the bottom on big screens. – progNewbie Aug 20 '18 at 20:25
  • thats great to hear. do you only need "Abmelden" to stick to the bottom? There are multiple ways of achieving that, it really depends on what exactly you need – StarterPack Aug 20 '18 at 21:14
  • @StarterPack Yes I only want to have "abmelden" stick to the bottom. – progNewbie Aug 20 '18 at 21:51
  • 1
    If that content is static just move it out of your recyclerview and add it to your layout – StarterPack Aug 21 '18 at 06:38

0 Answers0