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 の組み合わせを返す
こともできます。

元々の全ソースはここ

  1. public class MyExpandableListAdapter extends BaseExpandableListAdapter {  
  2.   
  3.     ....  
  4.   
  5.     public TextView getGenericView() {  
  6.         // Layout parameters for the ExpandableListView  
  7.         AbsListView.LayoutParams lp = new AbsListView.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, 64);  
  8.   
  9.         TextView textView = new TextView(ExpandableList1.this);  
  10.         textView.setLayoutParams(lp);  
  11.         // Center the text vertically  
  12.         textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);  
  13.         // Set the text starting position  
  14.         textView.setPadding(36000);  
  15.         return textView;  
  16.     }  
  17.           
  18.     public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {  
  19.         TextView textView = getGenericView();  
  20.         textView.setText(getChild(groupPosition, childPosition).toString());  
  21.         return textView;  
  22.     }  
  23.   
  24.     public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {  
  25.         TextView textView = getGenericView();  
  26.         textView.setText(getGroup(groupPosition).toString());  
  27.         return textView;  
  28.     }  
  29.   
  30.     ....  
  31. }  



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

  1. public class ExpandableListTest2 extends ExpandableListActivity {  
  2.   
  3.     MyExpandableListAdapter mAdapter;  
  4.   
  5.     @Override  
  6.     public void onCreate(Bundle savedInstanceState) {  
  7.         super.onCreate(savedInstanceState);  
  8.         setContentView(R.layout.mybooklist_layout);          
  9.   
  10.      setList();  
  11.     }  
  12.       
  13.     public void setList(){  
  14.      final int LIST_SIZE = 10;  
  15.        
  16.      int[] rowId = new int[LIST_SIZE];  
  17.      String[] parentList = new String[LIST_SIZE];  
  18.      String[][] childList = new String[LIST_SIZE][3];  
  19.        
  20.      for(int i=0; i<LIST_SIZE ; i++){  
  21.       rowId[i] = i;  
  22.       parentList[i] = "Title: " + i;  
  23.       childList[i][0] = "Author: " + i;  
  24.       childList[i][1] = "ISBN: " + i;  
  25.       childList[i][2] = "";  
  26.      }          
  27.           
  28.         mAdapter = new MyExpandableListAdapter(rowId, parentList, childList);  
  29.         setListAdapter(mAdapter);  
  30.         registerForContextMenu(getExpandableListView());  
  31.     }  
  32.   
  33.     public class MyExpandableListAdapter extends BaseExpandableListAdapter {  
  34.   
  35.      private int[] rowId;  
  36.      private String[] groups;  
  37.         private String[][] children;  
  38.           
  39.         public MyExpandableListAdapter(int[] rowId, String[] groups, String[][] children){  
  40.          this.rowId = rowId;  
  41.          this.groups = groups;  
  42.          this.children = children;  
  43.         }  
  44.           
  45.         public int getRowId(int groupPosition) {  
  46.          return rowId[groupPosition];  
  47.         }  
  48.           
  49.         public Object getChild(int groupPosition, int childPosition) {  
  50.             return children[groupPosition][childPosition];  
  51.         }  
  52.   
  53.         public long getChildId(int groupPosition, int childPosition) {  
  54.             return childPosition;  
  55.         }  
  56.   
  57.         public int getChildrenCount(int groupPosition) {  
  58.             return children[groupPosition].length;  
  59.         }  
  60.   
  61.         public TextView getGenericView() {  
  62.             AbsListView.LayoutParams lp = new AbsListView.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, getResources().getDimensionPixelSize(R.dimen.listHeight));  
  63.   
  64.             TextView textView = new TextView(ExpandableListTest2.this);  
  65.             textView.setLayoutParams(lp);  
  66.             textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);  
  67.             textView.setPadding(getResources().getDimensionPixelSize(R.dimen.listPadding), 000);  
  68.             return textView;  
  69.         }  
  70.           
  71.         public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {  
  72.   
  73.          // 最後の行は LinearLyout + Button を返す   
  74.          if(childPosition == getChildrenCount(groupPosition) - 1) {  
  75.                 AbsListView.LayoutParams lp = new AbsListView.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, getResources().getDimensionPixelSize(R.dimen.listHeight));  
  76.                 AbsListView.LayoutParams lp2 = new AbsListView.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);  
  77.   
  78.                 LinearLayout ll = new LinearLayout(ExpandableListTest2.this);  
  79.                 ll.setLayoutParams(lp);  
  80.                 ll.setPadding(getResources().getDimensionPixelSize(R.dimen.listPadding), 000);  
  81.                 ll.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);  
  82.   
  83.                 Button button = new Button(ExpandableListTest2.this);  
  84.                 button.setText("図書館検索");  
  85.                 button.setLayoutParams(lp2);  
  86.                 ll.addView(button);  
  87.                 return ll;  
  88.            
  89.          }  
  90.          else {  
  91.              TextView textView = getGenericView();  
  92.              textView.setText(getChild(groupPosition, childPosition).toString());  
  93.              return textView;  
  94.             
  95.          }  
  96.         }  
  97.           
  98.         public Object getGroup(int groupPosition) {  
  99.             return groups[groupPosition];  
  100.         }  
  101.   
  102.         public int getGroupCount() {  
  103.             return groups.length;  
  104.         }  
  105.   
  106.         public long getGroupId(int groupPosition) {  
  107.             return groupPosition;  
  108.         }  
  109.   
  110.         public View getGroupView(int groupPosition, boolean isExpanded, View convertView,  
  111.                 ViewGroup parent) {  
  112.             TextView textView = getGenericView();  
  113.             textView.setText(getGroup(groupPosition).toString());  
  114.             return textView;  
  115.         }  
  116.   
  117.         public boolean isChildSelectable(int groupPosition, int childPosition) {  
  118.             return true;  
  119.         }  
  120.   
  121.         public boolean hasStableIds() {  
  122.             return true;  
  123.         }  
  124.   
  125.     }  
  126. }  

0 件のコメント:

コメントを投稿