諸事情あって、ScrollView
の中に GridView
を配置するようなレイアウトのアプリ画面を作成していたのですが、この GridView
のスクロールを禁止して常にすべてのアイテムが展開された状態かつ、高さを wrap_content で指定したかったのでその手順のまとめです。
Android 標準の GridView では、上記のような環境で、要素の高さを android:layout_height="wrap_content"
と指定すると、一列目のみが表示され、二列目以降の要素はスクロールするようなレイアウトになってしまいます。
が、ScrollView の中でさらにスクロールするような要素は置きたくないし、そもそも GridView の要素はすべて見えるようにしたい!ということで、修正しました。
参考にさせていただいた記事はこちら。
AndroidのScrollViewの中のGridViewのlayout_heightが伸び縮みしないのを独自Viewで克服 · GitHub
https://gist.github.com/mistrydarshan99/d64966381331967e39cd
参考に…というか、コードをそのまま まるっと採用させていただきました。
GridView をカスタマイズした独自 View のコードはこちら。
なお、ファイル名は ExpandableHeightGridView.java としましたが、変更しても問題ありません。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | package com.cpointlab.example.view; import android.content.Context; import android.util.AttributeSet; import android.view.ViewGroup; import android.widget.GridView; /** * ScrollViewの中のGridViewでも高さを可変にする */ public class ExpandableHeightGridView extends GridView { boolean expanded = false ; public ExpandableHeightGridView(Context context) { super (context); } public ExpandableHeightGridView(Context context, AttributeSet attrs) { super (context, attrs); } public ExpandableHeightGridView(Context context, AttributeSet attrs, int defStyle) { super (context, attrs, defStyle); } public boolean isExpanded() { return expanded; } @Override public void onMeasure( int widthMeasureSpec, int heightMeasureSpec) { // HACK! TAKE THAT ANDROID! if (isExpanded()) { // Calculate entire height by providing a very large height hint. // View.MEASURED_SIZE_MASK represents the largest height possible. int expandSpec = MeasureSpec.makeMeasureSpec(MEASURED_SIZE_MASK, MeasureSpec.AT_MOST); super .onMeasure(widthMeasureSpec, expandSpec); ViewGroup.LayoutParams params = getLayoutParams(); params.height = getMeasuredHeight(); } else { super .onMeasure(widthMeasureSpec, heightMeasureSpec); } } public void setExpanded( boolean expanded) { this .expanded = expanded; } } |
パッケージ名は適宜環境に合わせて変更してください。
こちらのファイルをプロジェクトに追加したら、標準の GridView と置き換えます。
まず。レイアウトファイルのサンプルコードがこちら。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | < ScrollView android:id = "@+id/sc_spots" android:layout_width = "match_parent" android:layout_height = "wrap_content" android:fillViewport = "true" > < com.cpointlab.example.view.ExpandableHeightGridView android:id = "@+id/gridView" android:layout_width = "match_parent" android:layout_height = "wrap_content" android:verticalSpacing = "10dp" android:horizontalSpacing = "10dp" android:isScrollContainer = "false" android:numColumns = "3" android:stretchMode = "columnWidth" /> </ ScrollView > |
GridView を com.cpointlab.example.view.ExpandableHeightGridView
という View と置き換えています。
この時、10行目で高さを android:layout_height="wrap_content"
で指定しています。
次に、Activity 内のコードを下記のように変更します。
1 2 3 4 | ExpandableHeightGridView gridView = getView().findViewById(R.id.gridView); gridView.setExpanded( true ); gridView.setAdapter(adapter); adapter.notifyDataSetChanged(); |
Adapter
の定義については割愛しておりますが、変更はなかったのでそのままでOKです。
変更点は以上です。
あとは、アプリを再ビルドして実行します。
更新し忘れなどがなければ、問題なくスクロールなしですべての要素が既に展開された GridView が表示されているはずです。
最悪、高さを 400dp
などの固定値で指定すれば、同じことができなくもないのですが、要素が増えたときや他の端末で見たときにレイアウトが崩れる危険性があったので、この方法を採用しました。
そもそも、この方法の方が簡単かつ便利ですしね。
以上、Android アプリで ScrollView の中に配置した GridView の要素を全て展開し、かつ高さを wrap_content で指定する方法でした。
ご参考になれば幸いです。