LoginSignup
5
5

More than 5 years have passed since last update.

SimpleExpandableListAdapterを簡単に作成するライブラリ作った

Last updated at Posted at 2014-05-16

久しぶりに SimpleExpandableListAdapter を使って ExpandableListView 作ろうとしたら思った以上に面倒臭かったので、簡単に作成するためのライブラリを作ってみました。
ソースコード(GitHub)

ライブラリの使い方

そもそもですが、SimpleExpandaleListAdapter を作成するためには2つのコレクション GroupDataChildData を用意しなければいけません。
しかし、これらはネストされたジェネリック方になっており扱うの面倒でした。

  • GroupData … List<? extends Map<String, ?>>
  • ChildData … List<? extends List<? extends Map<String, ?>>>

アダプタに渡す際には上記のような型のコレクションにしなければいけませんが、元々のデータは次のようなシンプルな構造になっていると思います。

・Group1
    |__ Child1
    |__ Child2
    |__ Child3
    |
・Group2
    |__ Child1
    |__ Child2
    .
    . 

処理を書く際もこのような構造をもつコレクションでデータを管理しつつ、 このデータからアダプタを作れるようにしています。

クラス

  • GroupedStringList … グループ化された文字列データを格納する。
  • SimpleExpandableListAdapter … リストを元にアダプタを作成する。

使い方

親子関係のあるグループを作成する際、子項目は simple_expandable_list_item_1simple_expandable_list_item_2 に対応する個数の文字列を持つことができます。

// 子項目が1つの場合
GroupedStringList groupedList1 = new GroupedStringList();
List<String> languages = new ArrayList<String>();
languages.add("Java");
languages.add("C#");
languages.add("Python");
// 新しいグループを作成する
groupedList1.newGroupeOne("Programming", languages);

SimpleExpandableListAdapter adapter1 = GroupedAdapterBuilder.fromOne(groupedList1);

// 子項目が2つの場合
GroupedStringList groupedList2 = new GroupedStringList();
List<StringPair> companies = new ArrayList<StringPair>();
companies.add(new StringPair("Google", "1998/9/4"));
companies.add(new StringPair("Microsoft", "1975/4/4"));
companies.add(new StringPair("Apple", "1976/4/1"));
// 新しいグループを作成する
groupedList2.newGroupeTwo("Company", companies);

SimpleExpandableListAdapter adapter2 = GroupedAdapterBuilder.fromTwo(groupedList2);

このようにライブラリを使用することで、使うのが面倒な ExpandableListView がより簡単に使えるようになるのではないでしょうか。

5
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5