Android ExpandableListView的使用

展开型列表控件,原名ExpandableListView 是普通的列表控件进阶版, 可以自由的把列表进行收缩。
下面是实现后的截图:
Android <wbr>ExpandableListView的使用(展开型列表控件)

一、界面设计

该控件的实现需要一个主界面main.xml、一个标题界面groups.xml、以及一个列表内容界面childs.xml。

(1)首先我们来看看main.xml主界面,只需要一个ExpandableListView即可:
<LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android"
      android:layout_width= "fill_parent"
      android:layout_height= "fill_parent"
      android:orientation= "vertical" >

      <ExpandableListView
              android:id= "@id/android::list"
              android:layout_width= "fill_parent"
              android:layout_height= "fill_parent"
      />
</LinearLayout>

(2)标题界面groups.xml,只需要一个标题TextView控件上去即可。
<LinearLayout
      xmlns: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/textGroup"
              android:layout_width= "fill_parent"
              android:layout_height= "fill_parent"
              android:paddingLeft= "40px"
              android:paddingTop= "6px"
              android:paddingBottom= "6px"
              android:textSize= "15sp"
              android:text= "No data"
      />

</LinearLayout>

(3)列表内容控件childs.xml,是子控件,直接显示列表内容
<LinearLayout
      xmlns: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/textChild"
              android:layout_width= "fill_parent"
              android:layout_height= "fill_parent"
              android:paddingLeft= "60px"
              android:paddingTop= "10px"
              android:paddingBottom= "10px"
              android:textSize= "20sp"
                android:text= "No Data"
/>

</LinearLayout>

二、实现的主代码
下面给出具体的实现代码:
public class ExpandActivity extends ExpandableListActivity
{
     
      @Override
      public void onCreate( Bundle savedInstanceState)
      {
              super . onCreate( savedInstanceState);
              setContentView( R . layout . main);
             
              //创建二个一级条目标题
              Map < String , String > title_1 = new HashMap < String , String >();
              Map < String , String > title_2 = new HashMap < String , String >();
             
              title_1 . put( "group" , "开发");
              title_2 . put( "group" , "管理");
             
              //创建一级条目容器
              List < Map < String , String >> groups = new ArrayList < Map < String , String >>();
             
              groups . add( title_1);
              groups . add( title_2);
             
              //创建二级条目内容
             
              //内容一
              Map < String , String > content_1 = new HashMap < String , String >();
              Map < String , String > content_2 = new HashMap < String , String >();
             
              content_1 . put( "child" , "VC++");
              content_2 . put( "child" , "Java");
             
              List < Map < String , String >> childs_1 = new ArrayList < Map < String , String >>();
              childs_1 . add( content_1);
              childs_1 . add( content_2);
             
              //内容二
              Map < String , String > content_3 = new HashMap < String , String >();
              Map < String , String > content_4 = new HashMap < String , String >();
             
              content_3 . put( "child" , "敏捷开发");
              content_4 . put( "child" , "迭代开发");
             
              List < Map < String , String >> childs_2 = new ArrayList < Map < String , String >>();
              childs_2 . add( content_3);
              childs_2 . add( content_4);
             
              //存放两个内容, 以便显示在列表中
              List < List < Map < String , String >>> childs = new ArrayList < List < Map < String , String >>>();
              childs . add( childs_1);
              childs . add( childs_2);
             
              //创建ExpandableList的Adapter容器
              //参数: 1.上下文    2.一级集合    3.一级样式文件 4. 一级条目键值        5.一级显示控件名
              //         6. 二级集合    7. 二级样式    8.二级条目键值    9.二级显示控件名
              SimpleExpandableListAdapter sela = new SimpleExpandableListAdapter(
                              this , groups , R . drawable . groups , new String []{ "group" }, new int []{ R . id . textGroup },
                              childs , R . drawable . childs , new String []{ "child" }, new int []{ R . id . textChild }
                              );
             
              //加入列表
              setListAdapter( sela);
            //注册长按事件
        
registerForContextMenu(getExpandableListView());
      }
     
      //下面紧接着的2个方法设置长按事件
      //如何设置长按弹出来的Menu的Title是点击的位置的内容
      @Override
      public void onCreateContextMenu( ContextMenu menu , View v , ContextMenuInfo menuInfo) {
              menu . setHeaderTitle( "选择操作");
              menu . add( 0 , 0 , 0 , "聊天" );
              menu . add( 0 , 1 , 0 , "音视频监控");
              menu . add( 0 , 2 , 0 , "遥操作机器人");
              super . onCreateContextMenu( menu , v , menuInfo);
      }
     
      //上面的onCreateContextMenu方法
      //与下面的 onContextItemSelected 方法结合设置长按事件
      @Override
      public boolean onContextItemSelected( MenuItem item) {
              // TODO Auto-generated method stub
              //get the current ContentMenu item's id and title
              int itemID = item . getItemId();
              String itemTitle =( String) item . getTitle();
              //Toast.makeText(getApplicationContext(), "Your select context menu item is: "+itemTitle+ ",itemID:" + itemID+"",Toast.LENGTH_LONG).show();
             
              ExpandableListContextMenuInfo info = ( ExpandableListContextMenuInfo) item . getMenuInfo();     
              int type = ExpandableListView . getPackedPositionType( info . packedPosition);           
              if( type == ExpandableListView . PACKED_POSITION_TYPE_CHILD ){
                    int groupPos = ExpandableListView . getPackedPositionGroup( info . packedPosition);
                    int childPos = ExpandableListView . getPackedPositionChild( info . packedPosition);
                    Toast . makeText( ExpandActivity . this , "Child" + childPos + "点击的group" + groupPos , Toast . LENGTH_LONG ). show();
                    String currentItem = ( childs . get( groupPos ). get( childPos )). get( "child" ). toString();
                    Toast . makeText( ExpandActivity . this , current Item, Toast . LENGTH_LONG ). show();   
              } else if( type == ExpandableListView . PACKED_POSITION_TYPE_GROUP ){
                    //int groupPos = ExpandableListView.getPackedPositionGroup(info.packedPosition);
                    //Toast.makeText(FriendsActivity.this, "Group"+groupPos+"被电击了", Toast.LENGTH_LONG).show();
              }           
              return super . onContextItemSelected( item);
      }
     
     
      //列表内容按下
      @Override
      public boolean onChildClick( ExpandableListView parent , View v ,
                      int groupPosition , int childPosition , long id)
      {
              // TODO Auto-generated method stub
              //Toast.makeText(friendsListActivity.this, "content click,groupPosition:" + groupPosition + " childPosition:" + childPosition, Toast.LENGTH_SHORT).show();
              return super . onChildClick( parent , v , groupPosition , childPosition , id);
      }

      //二级标题按下
      @Override
      public boolean setSelectedChild( int groupPosition , int childPosition ,
                      boolean shouldExpandGroup)
      {
              // TODO Auto-generated method stub
              return super . setSelectedChild( groupPosition , childPosition , shouldExpandGroup);
      }

      //一级标题按下
      @Override
      public void setSelectedGroup( int groupPosition)
      {
              // TODO Auto-generated method stub
              super . setSelectedGroup( groupPosition);
      }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值