ExpandableList

一、ExpandableList

Activity代码:

public class ExpandableListActivityDemoActivity extends ExpandableListActivity {

    /** Called when the activity is first created. */

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        //声明第一个list对象

        List<Map<String,String>> groups=new ArrayList<Map<String,String>>();

        Map<String,String> map1=new HashMap<String,String>();

        map1.put("group", "中国");

        groups.add(map1);

        Map<String,String> map2=new HashMap<String,String>();

        map2.put("group", "日本");

        groups.add(map2);

       

      //为第一个一级条目提供二级条目的数据

        List<Map<String,String>> child1=new ArrayList<Map<String,String>>();

        Map<String,String> child1data=new HashMap<String,String>();

        child1data.put("child", "香港");

        child1.add(child1data);

        Map<String,String> child2data=new HashMap<String,String>();

        child2data.put("child", "甘肃");

        child1.add(child2data);

       

      //为第二个一级条目提供二级条目的数据

        List<Map<String,String>> child2=new ArrayList<Map<String,String>>();

        Map<String,String> child22data=new HashMap<String,String>();

        child22data.put("child", "北海道");

        child2.add(child22data);

        Map<String,String> child21data=new HashMap<String,String>();

        child21data.put("child", "北海道");

        child2.add(child21data);

       

        List<List<Map<String,String>>> childs=new ArrayList<List<Map<String,String>>>();

        childs.add(child1);

        childs.add(child2);

       

       

        SimpleExpandableListAdapter ada=new SimpleExpandableListAdapter(

                      this,

                      groups, R.layout.groups, new String[]{"group"}, new int[]{R.id.groups},

                      childs,R.layout.child,new String[]{"child"},new int[]{R.id.child}

                      );

        setListAdapter(ada);        

    }

 

       @Override

       public boolean onChildClick(ExpandableListView parent, View v,

                     int groupPosition, int childPosition, long id) {

              // TODO Auto-generated method stub

              Toast.makeText(this, groupPosition+"|"+childPosition, Toast.LENGTH_LONG).show();

              return super.onChildClick(parent, v, groupPosition, childPosition, id);

       }

   

}

groups.xml代码:

<?xmlversion="1.0"encoding="utf-8"?>

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    >

<TextView 

    android:id="@+id/groups" 

    android:layout_width="fill_parent" 

    android:layout_height="wrap_content" 

    android:text="No Data"

    android:padding="15px"

    android:layout_marginLeft="35px"

    />

</LinearLayout>

child.xml代码:

<?xmlversion="1.0"encoding="utf-8"?>

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    >

<TextView 

    android:id="@+id/child" 

    android:layout_width="fill_parent" 

    android:layout_height="wrap_content" 

    android:text="No Data"

    android:padding="10px"

    />

</LinearLayout>

main.xml代码:

<?xmlversion="1.0"encoding="utf-8"?>

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:paddingLeft="8dp"

    android:paddingRight="8dp"

    >

<ExpandableListViewandroid:id="@id/android:list"

    android:orientation="vertical"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"    

    />

</LinearLayout>

 

转载于:https://www.cnblogs.com/itfenqing/archive/2011/08/29/4429515.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值