2010年11月5日金曜日

Android 普通の LinearLayout で ExpandableListView っぽいのをつくる

前回のエントリ「Android ExpandableListView の子要素に EditText を入れるといろいろめんどい」で、ExpandableListView の中に EditText を入れるのは大変だと書きました。実は普通にやったほうが簡単だったりします。

キモは <include> タグ です。

こんなふうに同じレイアウトを何個も使う場合、<include> タグを使うとすっきりします。
<include> タグについては BootCamp のときにも話しました。
BootCamp を開催したよ。

サンプルでは、デフォルトのボタンを使ったのでダサいですが、そこはうまくカスタマイズしてください。



  1. package yanzm.example.fakeexpandablelisttest;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.View;  
  6. import android.widget.Button;  
  7. import android.widget.LinearLayout;  
  8.   
  9. public class FakeExpadableListTest extends Activity {  
  10.     @Override  
  11.     public void onCreate(Bundle savedInstanceState) {  
  12.         super.onCreate(savedInstanceState);  
  13.         setContentView(R.layout.main);  
  14.           
  15.         int groupIds[] = {R.id.group1, R.id.group2, R.id.group3, R.id.group4, R.id.group5, R.id.group6};  
  16.                   
  17.         for(int i = 0 ; i < groupIds.length; i++) {  
  18.             LinearLayout ll = (LinearLayout) findViewById(groupIds[i]);  
  19.             Button button = (Button)ll.findViewById(R.id.button);  
  20.             button.setOnClickListener(new View.OnClickListener() {  
  21.                 @Override  
  22.                 public void onClick(View v) {  
  23.                     LinearLayout ll = (LinearLayout)v.getParent();  
  24.                     LinearLayout groupll = (LinearLayout)ll.findViewById(R.id.inputgroup);  
  25.                     switch(groupll.getVisibility()) {  
  26.                         case View.GONE:  
  27.                             groupll.setVisibility(View.VISIBLE);  
  28.                             break;  
  29.                         case View.VISIBLE:  
  30.                             groupll.setVisibility(View.GONE);  
  31.                         break;  
  32.                     }  
  33.        
  34.                     ((Button)v).setText("Hidden Group");  
  35.                 }  
  36.             });  
  37.         }  
  38.     }  
  39. }  



main.xml
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7.     <include android:id="@+id/group1" layout="@layout/item" />  
  8.     <include android:id="@+id/group2" layout="@layout/item" />  
  9.     <include android:id="@+id/group3" layout="@layout/item" />  
  10.     <include android:id="@+id/group4" layout="@layout/item" />  
  11.     <include android:id="@+id/group5" layout="@layout/item" />  
  12.     <include android:id="@+id/group6" layout="@layout/item" />  
  13. </LinearLayout>  


item.xml
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:orientation="vertical"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="wrap_content"  
  5.     <  
  6.     <Button  
  7.         android:id="@+id/button"  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:text="Show Group"  
  11.         />  
  12.   
  13.     <LinearLayout  
  14.         android:id="@+id/inputgroup"  
  15.         android:orientation="vertical"  
  16.         android:layout_width="fill_parent"  
  17.         android:layout_height="wrap_content"  
  18.         android:paddingLeft="32dip"   
  19.         android:visibility="gone"  
  20.         >  
  21.     <LinearLayout  
  22.         android:orientation="horizontal"  
  23.         android:layout_width="fill_parent"  
  24.         android:layout_height="wrap_content"   
  25.         >  
  26.         <TextView  
  27.             android:layout_width="30dip"  
  28.             android:layout_height="wrap_content"   
  29.             android:text="住所"  
  30.             android:layout_marginLeft="5dip"  
  31.             />  
  32.         <EditText  
  33.             android:id="@+id/edittext1"  
  34.             android:layout_width="fill_parent"  
  35.             android:layout_height="wrap_content"   
  36.             android:inputType="text"  
  37.             />  
  38.     </LinearLayout>  
  39.     <LinearLayout  
  40.         android:orientation="horizontal"  
  41.         android:layout_width="fill_parent"  
  42.         android:layout_height="wrap_content"   
  43.         >  
  44.         <TextView  
  45.             android:layout_width="30dip"  
  46.             android:layout_height="wrap_content"   
  47.             android:text="氏名"  
  48.             android:layout_marginLeft="5dip"  
  49.             />  
  50.         <EditText  
  51.             android:id="@+id/edittext2"  
  52.             android:layout_width="fill_parent"  
  53.             android:layout_height="wrap_content"   
  54.             android:inputType="text"  
  55.             />  
  56.     </LinearLayout>  
  57.     <RadioGroup  
  58.         android:orientation="horizontal"  
  59.         android:layout_width="fill_parent"  
  60.         android:layout_height="wrap_content"       
  61.         >  
  62.         <RadioButton  
  63.             android:id="@+id/radio1"  
  64.             android:layout_width="wrap_content"  
  65.             android:layout_height="wrap_content"  
  66.             android:text="男性"  
  67.             />  
  68.         <RadioButton  
  69.             android:id="@+id/radio1"  
  70.             android:layout_width="wrap_content"  
  71.             android:layout_height="wrap_content"  
  72.             android:text="女性"  
  73.             />  
  74.     </RadioGroup>  
  75.     </LinearLayout>  
  76. </LinearLayout>  

0 件のコメント:

コメントを投稿