How to add a footer to gridview layout in android

2019-07-07 04:52发布

问题:

I need to add a footer view of some kind to a gridview layout in android, there is no official documentation that I can find for this and I have not been able to find a method that actually works in my google searches, has anyone achieved anything like this? I need to make a button that will show up the bottom of the gridview so that I can add more photos onclick, sort of like the below example from instagram, any help would go a long way thanks!

<?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="wrap_content"
android:orientation="vertical" >

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_margin="5dp" >

    <ImageView
        android:id="@+id/profile_user_avatar"
        android:layout_width="48px"
        android:layout_height="48px"
        android:adjustViewBounds="true"
        android:layout_marginTop="5dp" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" 
        android:paddingLeft="5dp">

        <TextView
            android:id="@+id/profile_realname"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="left"
            android:textSize="20dp"
            android:textStyle="bold"
             />

        <TextView
            android:id="@+id/profile_username"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="2dp"
            android:gravity="left" />
    </LinearLayout>
</LinearLayout>

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:layout_weight="1"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/tweets_stat"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@color/button_color_light"
            android:gravity="center"
            android:padding="10dp"
            android:textSize="20dp"
            android:textStyle="bold"
            android:textColor="#FFFFFF" />

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@color/button_color_dark"
            android:gravity="center"
            android:padding="10dp"
            android:text="Tweets"
            android:textSize="15dp"
            android:textStyle="bold"
            android:textColor="#FFFFFF" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:layout_weight="1"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/followers_stat"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@color/button_color_light"
            android:gravity="center"
            android:padding="10dp"
            android:textSize="20dp"
            android:textStyle="bold"
            android:textColor="#FFFFFF" />

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@color/button_color_dark"
            android:gravity="center"
            android:padding="10dp"
            android:text="Followers"
            android:textSize="15dp"
            android:textStyle="bold"
            android:textColor="#FFFFFF" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:layout_weight="1"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/following_stat"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@color/button_color_light"
            android:gravity="center"
            android:padding="10dp"
            android:textSize="20dp"
            android:textStyle="bold"
            android:textColor="#FFFFFF" />

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@color/button_color_dark"
            android:gravity="center"
            android:padding="10dp"
            android:text="Following"
            android:textSize="15dp"
            android:textStyle="bold"
            android:textColor="#FFFFFF" />
    </LinearLayout>
</LinearLayout>

<GridView
    android:id="@+id/gridView1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:columnWidth="90dp"
    android:gravity="center"
    android:horizontalSpacing="10dp"
    android:numColumns="auto_fit"
    android:stretchMode="columnWidth"
    android:verticalSpacing="10dp" >
</GridView>

<Button
    android:id="@+id/profile_button"
    style="@style/TextAppearance.Button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="30dp"
    android:layout_marginTop="30dp"
    android:background="@drawable/button_background"
    android:text="Load more photos" />
</LinearLayout>

回答1:

You could effectively add a footer grid item using something along the lines of: https://github.com/commonsguy/cwac-endless

This wouldn't look like what you require, it would look more like the last grid item is a button.

The other option is to have a relative layout which aligns the button to the bottom of the screen, initially its visibility is set to gone and is set to visible only when the user hits the end of the page. This is more of a hack/workaround.