简体   繁体   中英

Add a footer to a GridView Android

I am developing an android application for listing the products of an on-line store. For this I have used a GridView for that. But inside a category could be a big number of products, so I have to implement a feature like, "Loading on demand", with a button at the end of a gridView telling (Load More). Of course this button will be visible only if the GridView has reached the end of the items. But I dont how to do it. Below is my solution so far, but the button is not visible when gridview goes at the end.

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

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

        <com.twotoasters.jazzylistview.JazzyGridView
            android:id="@+id/ebuy_list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:drawSelectorOnTop="true"
            android:horizontalSpacing="15dp"
            android:numColumns="2"
            android:verticalSpacing="15dp" />

        <TextView
            android:id="@+id/more"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:layout_marginTop="10dp"
            android:background="@drawable/ebuy_now_background"
            android:clickable="true"
            android:onClick="loadMore"
            android:padding="15dp"
            android:text="Load More"
            android:textColor="@color/ebuy_color_second_strip"
            android:textSize="25sp" />

    </LinearLayout>

</ScrollView>

Suggestions or other solution will be more than welcome :)

使“加载更多”按钮成为GridView的一部分-当“位置”对应于最后一项时,在getView方法中,使用按钮使布局膨胀。

as you implement and add this listener to your Gridview this will detect for the end of Gridview as scroll position was at end of list just get more data. And during the loading data you need one flag to load data at once when the scroll position goes to end. So if data is loading and during that time you scroll up then scroll down then it will not get more data for dupplication.

    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
    this.currentFirstVisibleItem = firstVisibleItem;
    this.currentVisibleItemCount = visibleItemCount;
}

public void onScrollStateChanged(AbsListView view, int scrollState) {
    this.currentScrollState = scrollState;
    this.isScrollCompleted();
 }

private void isScrollCompleted() {
    if (this.currentVisibleItemCount > 0 && this.currentScrollState == SCROLL_STATE_IDLE) {
        /*** In this way I detect if there's been a scroll which has completed ***/
        /*** do the work for load more date! ***/
        if(!isLoading){
             isLoading = true;
             loadMoreDAta();
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM