Skip to content

Instantly share code, notes, and snippets.

@civic
Created January 22, 2012 13:14
Show Gist options
  • Save civic/1657035 to your computer and use it in GitHub Desktop.
Save civic/1657035 to your computer and use it in GitHub Desktop.
Extends BaseExpandableListAdapter
package test.civic;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import test.civic.AKB48.Member;
import android.app.Activity;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/*
* BaseExpandableListAdapterを継承した、AKB48データ用のAdapterを作成
*/
BaseExpandableListAdapter adapter = new BaseExpandableListAdapter() {
// とりあえずチームごとに振り分け
HashMap<String, List<Member>> teamMembersMap = new LinkedHashMap<String, List<Member>>();
List<String> teamList = new ArrayList<String>();
{
//データソースのAKB48から、アダプタで使いやすいように加工
// teamMembers: チーム名->メンバーのリスト
for (Member member : AKB48.getAkb48Raw()) {
String teamName = member.team;
List<Member> team = teamMembersMap.get(teamName);
if (team == null) {
team = new ArrayList<Member>();
}
team.add(member);
teamMembersMap.put(teamName, team);
}
//出現順を維持したチーム名のリスト
teamList.addAll(teamMembersMap.keySet());
}
@Override
public boolean isChildSelectable(int groupPosition,
int childPosition) {
return true;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
TextView label;
if (convertView == null) {
label = new TextView(parent.getContext());
label.setHeight(80);
label.setTextSize(32f);
label.setPadding(60, 10, 0, 0);
convertView = label;
} else {
label = (TextView) convertView;
}
label.setText(teamList.get(groupPosition));
return label;
}
@Override
public long getGroupId(int groupPosition) {
return (long) groupPosition; // position不変なのでIDと同一とする
}
@Override
public int getGroupCount() {
return teamList.size();
}
@Override
public Object getGroup(int groupPosition) {
return teamList.get(groupPosition);
}
@Override
public int getChildrenCount(int groupPosition) {
String team = teamList.get(groupPosition);
List<Member> list = teamMembersMap.get(team);
return list.size();
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
TextView label;
if (convertView == null) {
label = new TextView(parent.getContext());
label.setHeight(90);
label.setTextSize(24f);
label.setPadding(80, 0, 0, 0);
convertView = label;
} else {
label = (TextView) convertView;
}
String team = teamList.get(groupPosition);
Member member = teamMembersMap.get(team).get(childPosition);
label.setText(Html.fromHtml(member.name + "<br/><small>" + member.nickName + "</small>"));
return label;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return (long) (groupPosition * 100 + childPosition); //普遍データとして適当に生成
}
@Override
public Object getChild(int groupPosition, int childPosition) {
String team = teamList.get(groupPosition);
Member member = teamMembersMap.get(team).get(childPosition);
return member;
}
};
// インスタンス作成
ExpandableListView elvAkb48 = (ExpandableListView) findViewById(R.id.elv_akb48);
// Adapter登録
elvAkb48.setAdapter(adapter);
// =====とりあえずここまでで表示は出来ます=====
// メンバー部分のタップについてListener登録
elvAkb48.setOnChildClickListener(new OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
// Adapterをもらってくる
ExpandableListAdapter adapter = parent.getExpandableListAdapter();
// メンバー表示用データ作成時に作ったブツがもらえます
Member member = (Member)adapter.getChild(groupPosition, childPosition);
// 表示に使わなかったbirthday要素も表示できます☆-(ノ゚Д゚)八(゚Д゚ )ノイエーイ
Toast.makeText(MainActivity.this, member.birthDay, Toast.LENGTH_SHORT).show();
return false;
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment