2014-09-01 57 views
0

我正在开发一个用于列出在线商店产品的Android应用程序。为此,我使用了GridView。但是在一个类别中可能会有很多产品,所以我必须实现一个功能,例如“按需加载”,并在gridView结尾告诉(加载更多)按钮。当然这个按钮只有在GridView已达到物品末尾时才可见。但我不知道如何去做。以下是我目前的解决方案,但在末尾处出现gridview时,该按钮不可见。添加页脚到GridView Android

<?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> 

建议或其他解决方案会比欢迎:)更

+0

http://stackoverflow.com/questions/8876596/need-to-create-footer-for-gridview-in-android – 2014-09-01 09:06:27

+0

@ user1728071这个我用过的布局,你的意思了布局GridView的项目 – Xhulio 2014-09-01 09:12:13

回答

0

当你实现并添加这个监听器到你的Gridview时,这将检测Gridview的结束,因为滚动位置在列表的末尾只是获得更多的数据。在加载数据期间,当滚动位置结束时,您需要一个标志来一次加载数据。因此,如果数据正在加载,并在此期间向上滚动,然后向下滚动,则不会获得更多的数据。

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(); 
     } 
    } 
} 
+0

我认为这是最好的解决方案,因为'GridView'的默认行为不支持页眉和页脚。 – Xhulio 2014-09-01 09:59:06

0

使“加载更多”在GridView的按钮部分 - 在getView方法用充气按钮布局时,“位置”对应于最后一个项目。