やりたいこと
GridViewを一番下までスクロールした際に、GridViewのItemを追加したいです。
過去にListViewで、上記のような実装をした経験は一応あります。
問題点
ScrollView内に、GridViewを入れて、そのGridViewにonScrollListenerを実装しonScrollで判定しようと思いましたが上手くいきませんでした。
ありきたりなコードは下記を参照願います。
java
1@Override 2public void onScroll(AbsListView absListView, 3 int firstVisibleItem, // index of first visible item 4 int visibleItemCount, // count of visible list item 5 int totalItemCount) { // count total of all list item 6 // bottom 7 if (totalItemCount == firstVisibleItem + visibleItemCount && firstVisibleItem != 0) { 8 // GridViewの追加読み込み処理へ遷移 9 } 10 }
怪しいと思う点
ScrollView内にGridViewがあることで、画面に表示されているGridViewのItemのみではなく、端末で表示されていないViewまで、visibleItemCountとして認識しているため、判定処理を突破してしまいGridViewの追加読み込み処理が再度発生して、取得し続けるようになっているように見えています。
コード
xml
1<?xml version="1.0" encoding="utf-8"?> 2<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 xmlns:app="http://schemas.android.com/apk/res-auto" 5 tools:context="com.hogehoge.hogehoge.AnotherProfileActivity" 6 android:layout_width="match_parent" 7 android:layout_height="match_parent" 8 android:orientation="vertical" 9 android:background="@color/theme700"> 10 <android.support.v7.widget.Toolbar 11 android:layout_width="match_parent" 12 android:layout_height="wrap_content" 13 android:background="@color/toolbar_background" 14 android:theme="@style/AppTheme" 15 app:popupTheme="@style/Theme.AppCompat.Light"/> 16 <ScrollView 17 android:layout_width="match_parent" 18 android:layout_height="wrap_content" 19 android:orientation="vertical" > 20 <LinearLayout 21 android:layout_width="match_parent" 22 android:layout_height="wrap_content" 23 android:orientation="vertical" > 24 <LinearLayout 25 ここにもLayoutを作成しています 26 </LinearLayout> 27 <LinearLayout 28 android:layout_width="match_parent" 29 android:layout_height="wrap_content" 30 android:orientation="vertical" 31 android:paddingTop="3dp" > 32 <com.hogehoge.hogehoge.ExpandableHeightGridView 33 android:layout_width="match_parent" 34 android:layout_height="wrap_content" 35 android:gravity="center" 36 android:horizontalSpacing="1dp" 37 android:verticalSpacing="1dp" 38 android:numColumns="3" 39 android:stretchMode="columnWidth" /> 40 </LinearLayout> 41 </LinearLayout> 42 </ScrollView> 43</RelativeLayout>
xml
1import android.content.Context; 2import android.util.AttributeSet; 3import android.view.ViewGroup; 4import android.widget.GridView; 5 6public class ExpandableHeightGridView extends GridView { 7 8 boolean expanded = false; 9 10 public ExpandableHeightGridView(Context context) { 11 super(context); 12 } 13 14 public ExpandableHeightGridView(Context context, AttributeSet attrs) { 15 super(context, attrs); 16 } 17 18 public ExpandableHeightGridView(Context context, AttributeSet attrs, int defStyle) { 19 super(context, attrs, defStyle); 20 } 21 22 public boolean isExpanded() { 23 return expanded; 24 } 25 26 @Override 27 public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 28 // HACK! TAKE THAT ANDROID! 29 if (isExpanded()) { 30 // Calculate entire height by providing a very large height hint. 31 // View.MEASURED_SIZE_MASK represents the largest height possible. 32 int expandSpec = MeasureSpec.makeMeasureSpec(MEASURED_SIZE_MASK, MeasureSpec.AT_MOST); 33 super.onMeasure(widthMeasureSpec, expandSpec); 34 35 ViewGroup.LayoutParams params = getLayoutParams(); 36 params.height = getMeasuredHeight(); 37 } 38 else { 39 super.onMeasure(widthMeasureSpec, heightMeasureSpec); 40 } 41 } 42 43 public void setExpanded(boolean expanded) { 44 this.expanded = expanded; 45 } 46}
以上、このような問題を経験したことがある方がおりましたら
コメント・ご協力をよろしくお願い致します。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。