2010年8月23日月曜日

Android Expandable List のビューをカスタマイズ

Expandable List の子要素の最後をボタンにしてみました。

こんな感じ。



# 次の Libraroid のアップデートに組み込むつもり~

以前のエントリAndroid Expandable List を使うで、Expandable List を使うには、SimpleExpandableListAdapter を使うのが簡単だと書きましたが、今回のようにカスタマイズする場合は使えません。

参考にしたのが、ApiDemo の ExpandableList1.java です。

BaseExpandableListAdapter を継承して独自の Adapter を作ります。

キモは
 getChildView()getGroupView() です。

ここで返している View が対応する位置の行の View になります。
ここでは TextView を返していますが、Button や ImageView を
返せば、それが行のViewになります。
もちろん LinearLayout を使って複数の View の組み合わせを返す
こともできます。

元々の全ソースはここ


public class MyExpandableListAdapter extends BaseExpandableListAdapter {

....

public TextView getGenericView() {
// Layout parameters for the ExpandableListView
AbsListView.LayoutParams lp = new AbsListView.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, 64);

TextView textView = new TextView(ExpandableList1.this);
textView.setLayoutParams(lp);
// Center the text vertically
textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
// Set the text starting position
textView.setPadding(36, 0, 0, 0);
return textView;
}

public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
TextView textView = getGenericView();
textView.setText(getChild(groupPosition, childPosition).toString());
return textView;
}

public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
TextView textView = getGenericView();
textView.setText(getGroup(groupPosition).toString());
return textView;
}

....
}



で、今回は子要素の最後の列を LinearLyout + Button にしてみました。


public class ExpandableListTest2 extends ExpandableListActivity {

MyExpandableListAdapter mAdapter;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mybooklist_layout);

setList();
}

public void setList(){
final int LIST_SIZE = 10;

int[] rowId = new int[LIST_SIZE];
String[] parentList = new String[LIST_SIZE];
String[][] childList = new String[LIST_SIZE][3];

for(int i=0; i<LIST_SIZE ; i++){
rowId[i] = i;
parentList[i] = "Title: " + i;
childList[i][0] = "Author: " + i;
childList[i][1] = "ISBN: " + i;
childList[i][2] = "";
}

mAdapter = new MyExpandableListAdapter(rowId, parentList, childList);
setListAdapter(mAdapter);
registerForContextMenu(getExpandableListView());
}

public class MyExpandableListAdapter extends BaseExpandableListAdapter {

private int[] rowId;
private String[] groups;
private String[][] children;

public MyExpandableListAdapter(int[] rowId, String[] groups, String[][] children){
this.rowId = rowId;
this.groups = groups;
this.children = children;
}

public int getRowId(int groupPosition) {
return rowId[groupPosition];
}

public Object getChild(int groupPosition, int childPosition) {
return children[groupPosition][childPosition];
}

public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}

public int getChildrenCount(int groupPosition) {
return children[groupPosition].length;
}

public TextView getGenericView() {
AbsListView.LayoutParams lp = new AbsListView.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, getResources().getDimensionPixelSize(R.dimen.listHeight));

TextView textView = new TextView(ExpandableListTest2.this);
textView.setLayoutParams(lp);
textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
textView.setPadding(getResources().getDimensionPixelSize(R.dimen.listPadding), 0, 0, 0);
return textView;
}

public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {

// 最後の行は LinearLyout + Button を返す
if(childPosition == getChildrenCount(groupPosition) - 1) {
AbsListView.LayoutParams lp = new AbsListView.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, getResources().getDimensionPixelSize(R.dimen.listHeight));
AbsListView.LayoutParams lp2 = new AbsListView.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

LinearLayout ll = new LinearLayout(ExpandableListTest2.this);
ll.setLayoutParams(lp);
ll.setPadding(getResources().getDimensionPixelSize(R.dimen.listPadding), 0, 0, 0);
ll.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);

Button button = new Button(ExpandableListTest2.this);
button.setText("図書館検索");
button.setLayoutParams(lp2);
ll.addView(button);
return ll;

}
else {
TextView textView = getGenericView();
textView.setText(getChild(groupPosition, childPosition).toString());
return textView;

}
}

public Object getGroup(int groupPosition) {
return groups[groupPosition];
}

public int getGroupCount() {
return groups.length;
}

public long getGroupId(int groupPosition) {
return groupPosition;
}

public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
ViewGroup parent) {
TextView textView = getGenericView();
textView.setText(getGroup(groupPosition).toString());
return textView;
}

public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}

public boolean hasStableIds() {
return true;
}

}
}

0 件のコメント:

コメントを投稿