Bar Mounted to bottom of screen if listview overflows

时间:2015-05-24 21:49:19

标签: android listview android-listview android-linearlayout

I want the bottom_control_bar to be mounted to the bottom of the listview, however if the listview overflows and has to be scrolled through I want it to be mounted to the bottom of the screen so that it is always visible. Currently if the listView doesnt fill the whole page, everything is fine, however if it overflows the bottom_control_bar isn't visible at all.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/grey05"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.albert.betterapp.MyBets">

<LinearLayout
    android:id="@+id/top_control_bar"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:background="#C9C193"
    android:orientation="horizontal"
    android:weightSum="1">

<TextView
    android:id="@+id/textView91"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="0.5"
    android:text="Accumulator(5)"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:textSize="16dp"
    android:textStyle="bold" />

<TextView
    android:id="@+id/textView101"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="0.3"
    android:text="Token:"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:textSize="16dp"
    android:textStyle="bold" />
<TextView
    android:id="@+id/textView121"
    android:layout_width="wrap_content"
    android:layout_height="18dp"
    android:layout_gravity="top"
    android:layout_weight="0.2"
    android:layout_marginBottom="-5dp"
    android:includeFontPadding="false"
    android:text=""
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:textColor="@color/wingreen"
    android:textStyle="bold" />


</LinearLayout>
<LinearLayout
    android:id="@+id/bottom_control_bar"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/linearLayout23"
    android:background="#C9C193"
    android:orientation="horizontal"
    android:weightSum="1">

    <TextView
        android:id="@+id/textView9"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.2"
        android:text="@string/stake"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textSize="16dp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/textView10"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.3"
        android:text="500"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textSize="16dp"
        android:textStyle="bold" />
    <TextView
        android:id="@+id/textView12"
        android:layout_width="wrap_content"
        android:layout_height="18dp"
        android:layout_gravity="top"
        android:layout_weight="0.2"
        android:layout_marginBottom="-5dp"
        android:includeFontPadding="false"
        android:text="@string/potentialwinnings"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="@color/wingreen"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/textView13"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.3"
        android:text="6000"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textSize="16dp"
        android:textStyle="bold" />



</LinearLayout>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearLayout23"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_below="@id/top_control_bar"
    android:orientation="vertical">
    <!-- Main ListView
         Always give id value as list(@android:id/list)
    -->
    <ListView
        android:id="@+id/betslistview"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@color/betcolor"></ListView>

</LinearLayout>


</RelativeLayout>

1 个答案:

答案 0 :(得分:1)

The best option here is to add a footer to the ListView when all the elements don't exceed the screen height. If the sum of all the ListView item's height is greater than the screen height you switch the layout type for one where you have a ViewGroup (Relative Layout) with a ListView (without the footer) on the top and a LinearLayout at the bottom (the desired footer).

EDIT:
- Create a isolated layout with your bottom_control_bar;
- Create a layout with a ListView and a LinearLayout bellow where you include (in your XML) that bottom_control_bar;
- In your Activity onCreate you should implement the logic I've explained above and if:

A - your list height doesn't exceeds the screen you should set your bottom_control_bar visibility to GONE and add that bottom_control_bar as a header to your ListView.

B - your list height exceeds the screen you don't add a header to your ListView and don't change the bottom_control_bar visibility to GONE, in other words, you do nothing to your layout.