質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.47%
Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

Q&A

解決済

1回答

2781閲覧

ListViewにGridViewを埋め込むAdapter

TTYTT

総合スコア9

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

0グッド

0クリップ

投稿2020/06/08 09:35

編集2020/06/08 09:44

GridViewに見出し

GridViewに見出しを付け、この図のような画面を作りたいと考えております。
将来的にはこの見出し部分にボタンも埋め込みたいと考えております。

色々と検索して見つかった以下のページを参考に、ListViewにGridViewを埋め込む方式のものを作成しているのですが、こちらのページでは足りないクラスやxmlレイアウトがあるようで、どのようなものを作成すればいいのか調べながら理解しようとしましたが、解決出来ませんでした。
https://qiita.com/KeithYokoma/items/a479ee529c520e829e7f

Java

1public class SectionListAdapter extends ArrayAdapter<SectionData> { 2 public SectionListAdapter(Context context, List<SectionData> objects) { 3 super(context, R.layout.list_item_layout, R.id.label_section_title, objects); 4 } 5 6 @Nullable 7 @Override 8 public View getView(int position, View convertView, ViewGroup group) { 9 View row = super.getView(position, convertView, group); 10 11 SectionData data = getItem(position); 12 13 TextView sectionName = (TextView) row.findViewById(R.id.label_section_name); 14 sectionName.setText(data.getSectionName()); 15 16 GridView grid = (GridView) row.findViewById(R.id.grid); 17 SectionGridAdapter adapter = new SectionGridAdapter(getContext(), data.getImageUrls()); 18 grid.setAdapter(adapter); 19 20 return row; 21 } 22}

↑こちらのR.id.label_section_titleはlist_item_layout.xml内に記述するべき何かなのでしょうか。
また、getSectionName、getImageUrlsの定義や取得する値についてはどのように記述すればいいのでしょうか。

Java

1public class SectionGridAdapter extends ArrayAdapter<String> { 2 public SectionGridAdapter(Context context, List<String> objects) { 3 super(context, R.layout.grid_item, R.id.label_grid, objects); 4 } 5 6(省略)

↑こちらのR.id.label_gridも同様に理解ができません。

↓自分で追加したxmlレイアウトです。

list_item_layout.xml

xml

1<?xml version="1.0" encoding="utf-8"?> 2<androidx.constraintlayout.widget.ConstraintLayout 3 xmlns:android="http://schemas.android.com/apk/res/android" 4 xmlns:app="http://schemas.android.com/apk/res-auto" 5 xmlns:tools="http://schemas.android.com/tools" 6 android:layout_width="match_parent" 7 android:layout_height="match_parent"> 8 9 <TextView 10 android:id="@+id/label_section_name" 11 android:layout_width="wrap_content" 12 android:layout_height="wrap_content" 13 android:text="TextView" 14 tools:ignore="MissingConstraints" 15 tools:layout_editor_absoluteX="16dp" 16 tools:layout_editor_absoluteY="16dp" /> 17 18 <GridView 19 android:id="@+id/grid" 20 android:layout_width="wrap_content" 21 android:layout_height="22dp" 22 app:layout_constraintEnd_toEndOf="parent" 23 app:layout_constraintHorizontal_bias="0.0" 24 app:layout_constraintStart_toStartOf="parent" 25 tools:ignore="MissingConstraints" 26 tools:layout_editor_absoluteY="35dp" /> 27 28 29</androidx.constraintlayout.widget.ConstraintLayout>

grid_item.xml

xml

1<?xml version="1.0" encoding="utf-8"?> 2<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 xmlns:app="http://schemas.android.com/apk/res-auto"> 6 7 <ImageView 8 android:id="@+id/image_cell" 9 android:layout_width="240dp" 10 android:layout_height="240dp" 11 android:padding="3dp" 12 app:layout_constraintStart_toStartOf="parent" 13 app:layout_constraintTop_toTopOf="parent" /> 14 15</androidx.constraintlayout.widget.ConstraintLayout>

以上の情報で何かお分かりでしたらご教授頂きたく思います。宜しくお願い致します。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

ListView よりも RecyclerView と GridLayoutManager を組み合わせて使った方がいいと思います。
サンプルプロジェクト を作ってみたのでご覧ください。以下、解説します。

まず MainActivity より、

java

1 List<String> sectionTitles = new ArrayList<>(); 2 List<List<String>> sectionItems = new ArrayList<>();

見出しと表示項目のデータを作ってます。サンプルなのでどちらも単なる文字列ですが、実際には Section とか Item みたいなクラスを作る (かつ、各セクションの項目は Section に持たせる) のがお勧めです。
この後の for 文で、セクション 10 個と、各セクションにランダムに 1 〜 20 個の項目を作ってます。

java

1 GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 3); 2 gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() { 3 @Override 4 public int getSpanSize(int position) { 5 if (adapter.getSectionItemIndex(position).length == 1) { 6 return 3; 7 } else { 8 return 1; 9 } 10 } 11 });

GridLayoutManager というのを使うと横にいくつ項目を並べるかを指定できます。また、SpanSizeLookup を設定すると、項目ごとの横幅を指定できますので、見出しの場合は横いっぱいに表示するようにしています。
参考: android - RecyclerView GridLayoutManager with full width header - Stack Overflow

次に SectionGridAdapter ですが、RecyclerView の Adapter は (某 iOS の TableView と違って) 単なる 1 次元配列を想定しているので、例えば以下のようなデータの場合、まず getItemCount で合計項目数 (count = 9) を計算する必要があります。また、Adapter のメソッドは 0 〜 count - 1 の position の値を使って呼ばれるので、それがどのセクションの何番目の項目かを getSectionItemIndex で計算しています。

├── Section 1 position = 0 index = [0] │   └── Item 1-1 position = 1 index = [0, 0] ├── Section 2 position = 2 index = [1] │   ├── Item 2-1 position = 3 index = [1, 0] │   └── Item 2-2 position = 4 index = [1, 1] └── Section 3 position = 5 index = [2] ├── Item 3-1 position = 6 index = [2, 0] ├── Item 3-2 position = 7 index = [2, 1] └── Item 3-3 position = 8 index = [2, 2] 合計項目数 count = 9

java

1 // 合計項目数 (セクションの数 + 各セクションの項目数の合計) を計算する。 2 @Override 3 public int getItemCount() { 4 int count = sectionTitles.size(); 5 for (List<String> items : sectionItems) { 6 count += items.size(); 7 } 8 return count; 9 } 10 11 // position が見出しの場合は長さ 1 の配列 [セクション番号]、 12 // 項目の場合は長さ 2 の配列 [セクション番号, 項目番号] を返す。 13 public int[] getSectionItemIndex(int position) { 14 for (int section = 0; section < sectionTitles.size(); section++) { 15 if (position == 0) { 16 return new int[] { section }; 17 } else if (position <= sectionItems.get(section).size()) { 18 return new int[] { section, position - 1 }; 19 } 20 position -= 1 + sectionItems.get(section).size(); 21 } 22 return null; 23 }

あとは普通の RecyclerView ですね。見出しと項目はそれぞれ layout_headerlayout_item で作ってますので、必要に応じて修正してください。

投稿2020/06/08 21:15

編集2020/06/08 21:28
hoshi-takanori

総合スコア7895

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

TTYTT

2020/06/10 10:12 編集

ご丁寧に解説やサンプルまで頂き、とても理解がしやすく大変助かりました。ありがとうございます。 RecyclerViewというものの存在を知らずにListViewを使う方法しかないと思い込んでいたため、幅が広がり大変勉強になりました。 頂いた情報を参考に見出し(Section)と画像(Item)を、想像していた通りの一覧配置で表示することができました。 これを元にそれぞれのItemにClickListnerを設定する際は、こちらのサンプルの場合のItemViewHolderクラス内に記述をすれば宜しいのでしょうか。
hoshi-takanori

2020/06/10 10:50

はい。各項目が押された場合の処理は ItemViewHolder の bindItem, 見出しが押された場合の処理は HeaderViewHolder の bindHeader に書けばいいと思います。
TTYTT

2020/06/11 06:24

なんとか構造を理解し先に進むことが出来そうです、大変勉強になりました。ありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.47%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問