(ExpandableListView)android中树形组件的使用

http://hi.baidu.com/vip_qining/item/b8797fd4b96c828f270ae73d

ListView:为用户提供了列表显示的功能,但是如果想要对这些列表数据进行分组管理就需要使用ExpandableListView 完成。

ExpandableListView:是ListView扩充的组件,既然这个组件也是列表的功能,那么就有一个操作可以设置数据,可以通过setAdapter()方法设置数据,可是这次的setAdapter()方法中,可以接受数据类型。 如果要设置数据就要定义一个类,这个类继承BaseExpandableListAdapter

1.编写MyExpandableAdapter继承BaseExpandableListAdapter

package com.qnpop;

import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

public class MyExpandableAdapter extends BaseExpandableListAdapter {
 private String [] groups=new String[]{"我的朋友","我的家人","过去回忆","黑名单"};//组名称
 private String [][] children=new String[][]{{"齐宁","穆艳丽","羊羔子","过去了"}
 ,{"父亲","母亲","哥哥","姐姐","姐夫","孩子们"},{"张三","里斯","王五"},
 {"傻逼","兆佳","没良心","不是人"}};//分组的选项
 private Context context;
      public MyExpandableAdapter(Context context) {
  this.context = context;
 }
 //获得指定组中的指定索引的子选项数据
 public Object getChild(int groupPosition, int childPosition) {
  return children[groupPosition][childPosition];
 }
     //获得指定子项的ID
 public long getChildId(int groupPosition, int childPosition) {
  return childPosition;
 }
     //获得指定子项的view组件
 public View getChildView(int groupPosition, int childPosition,
   boolean isLastChild, View convertView, ViewGroup parent) {
  TextView txt=builderTextView();
  txt.setText(getChild(groupPosition,childPosition).toString());
  return txt;
 }
    //取得指定组中所有子项的个数
 public int getChildrenCount(int groupPosition) {
  return children[groupPosition].length;
 }
     //取得指定组的数据
 public Object getGroup(int groupPosition) {
  return groups[groupPosition];
 }
  //取得指定组的个数
 public int getGroupCount() {
  return groups.length;
 }
  //取得指定索引的ID
 public long getGroupId(int groupPosition) {
  return groupPosition;
 }
  //取得指定组的View组件
 public View getGroupView(int groupPosition, boolean isExpanded,
   View convertView, ViewGroup parent) {
  TextView textView=builderTextView();
  textView.setText(getGroup(groupPosition).toString());
  return textView;
 }
      //如果返回true表示子项和组的ID始终表示一个固定的组件对象
 public boolean hasStableIds() {
  return true;
 }
//判断指定的子选择项是否被选择
 public boolean isChildSelectable(int groupPosition, int childPosition) {
  return true;
 }
 private TextView builderTextView(){
  AbsListView.LayoutParams param=new AbsListView.LayoutParams(
    ViewGroup.LayoutParams.FILL_PARENT,200);
  TextView txt=new TextView(context);
  txt.setLayoutParams(param);
  txt.setTextSize(15.0f);
  txt.setGravity(Gravity.LEFT);
  txt.setPadding(40, 8, 3, 3);
  return txt;
 }

}

2.此时一个专门用于数据填充的组件就完成了,完成后肯定要在组件上配置数据,而这个配置功能就需要布局文件完成

<?xml version="1.0" encoding="utf-8"?>
<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/expand"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        />

</LinearLayout>

 

3.编写代码完成功能

package com.qn.expand;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ExpandableListView;

public class MainActivity extends Activity {
    /** Called when the activity is first created. */
 private ExpandableListView eListView;
 private MyExpandableAdapter adapter;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        eListView=(ExpandableListView) findViewById(R.id.expand);
        adapter=new MyExpandableAdapter(MainActivity.this);
        eListView.setAdapter(adapter);
    }
}

 

结果

事例二

上面基本显示虽然完成了但是并没有太大的用途,而在组件之中也提供了各个操作处理的方法。

如下值在Activity中添加监听事件

package com.qn.expand;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.ExpandableListView.OnGroupCollapseListener;
import android.widget.ExpandableListView.OnGroupExpandListener;
import android.widget.Toast;

public class MainActivity extends Activity {
    /** Called when the activity is first created. */
 private ExpandableListView eListView;
 private MyExpandableAdapter adapter;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        eListView=(ExpandableListView) findViewById(R.id.expand);
        adapter=new MyExpandableAdapter(MainActivity.this);
        eListView.setAdapter(adapter);
        eListView.setOnChildClickListener(new onChildClickListenerImpl());//子被选中
        eListView.setOnGroupClickListener(new onGroupClickListenerImpl());//分组被选中
        eListView.setOnGroupExpandListener(new onGroupExpandListenerImpl());//组展开操作
        eListView.setOnGroupCollapseListener(new onGroupCollapseListenerImpl());//组关闭的操作
    }
    private class onChildClickListenerImpl implements OnChildClickListener{

  public boolean onChildClick(ExpandableListView parent, View v,
    int groupPosition, int childPosition, long id) {
   Toast.makeText(MainActivity.this, "子被选中,groupPosition="+groupPosition+"" +
     ",childPosition="+childPosition,
     Toast.LENGTH_SHORT).show();
   return false;
  }
     
    }
    private class onGroupClickListenerImpl implements OnGroupClickListener{

  public boolean onGroupClick(ExpandableListView parent, View v,
    int groupPosition, long id) {
   Toast.makeText(MainActivity.this, "组被选中,groupPosition="+groupPosition,
     Toast.LENGTH_SHORT).show();
   return false;
  }
     
     
    }
    private class onGroupExpandListenerImpl implements OnGroupExpandListener{

  public void onGroupExpand(int groupPosition) {
   Toast.makeText(MainActivity.this, "打开分组,groupPosition="+groupPosition,
     Toast.LENGTH_SHORT).show();
  }
     
     
    }
    private class onGroupCollapseListenerImpl implements OnGroupCollapseListener{

  public void onGroupCollapse(int groupPosition) {
   Toast.makeText(MainActivity.this, "关闭分组,groupPosition="+groupPosition,
     Toast.LENGTH_SHORT).show();
  }
     
     
    }
}

结果


事例三

很多情况下往往是对一个子项进行操作 如果要对一个子项操作,最简单的方法是使用上下文菜单。首先     registerForContextMenu(eListView);//注册事件

下面在Activity中编写代码

package com.qn.expand;

import android.app.Activity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.ExpandableListContextMenuInfo;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.ExpandableListView.OnGroupCollapseListener;
import android.widget.ExpandableListView.OnGroupExpandListener;
import android.widget.Toast;

public class MainActivity extends Activity {
    /** Called when the activity is first created. */
 private ExpandableListView eListView;
 private MyExpandableAdapter adapter;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        eListView=(ExpandableListView) findViewById(R.id.expand);
        adapter=new MyExpandableAdapter(MainActivity.this);
        eListView.setAdapter(adapter);
        registerForContextMenu(eListView);//注册事件
        eListView.setOnChildClickListener(new onChildClickListenerImpl());//子被选中
        eListView.setOnGroupClickListener(new onGroupClickListenerImpl());//分组被选中
        eListView.setOnGroupExpandListener(new onGroupExpandListenerImpl());//组展开操作
        eListView.setOnGroupCollapseListener(new onGroupCollapseListenerImpl());//组关闭的操作
    }
    private class onChildClickListenerImpl implements OnChildClickListener{

  public boolean onChildClick(ExpandableListView parent, View v,
    int groupPosition, int childPosition, long id) {
   Toast.makeText(MainActivity.this, "子被选中,groupPosition="+groupPosition+"" +
     ",childPosition="+childPosition,
     Toast.LENGTH_SHORT).show();
   return false;
  }
     
    }
    private class onGroupClickListenerImpl implements OnGroupClickListener{

  public boolean onGroupClick(ExpandableListView parent, View v,
    int groupPosition, long id) {
   Toast.makeText(MainActivity.this, "组被选中,groupPosition="+groupPosition,
     Toast.LENGTH_SHORT).show();
   return false;
  }
     
     
    }
    private class onGroupExpandListenerImpl implements OnGroupExpandListener{

  public void onGroupExpand(int groupPosition) {
   Toast.makeText(MainActivity.this, "打开分组,groupPosition="+groupPosition,
     Toast.LENGTH_SHORT).show();
  }
     
     
    }
    private class onGroupCollapseListenerImpl implements OnGroupCollapseListener{

  public void onGroupCollapse(int groupPosition) {
   Toast.makeText(MainActivity.this, "关闭分组,groupPosition="+groupPosition,
     Toast.LENGTH_SHORT).show();
  }
     
     
    }
 @Override
 public void onCreateContextMenu(ContextMenu menu, View v,
   ContextMenuInfo menuInfo) {
  super.onCreateContextMenu(menu, v, menuInfo);
  ExpandableListView.ExpandableListContextMenuInfo info=(ExpandableListContextMenuInfo) menuInfo;
  //取得菜单项
  int type=ExpandableListView.getPackedPositionType(info.packedPosition);
  //取得所在组的索引
  int group=ExpandableListView.getPackedPositionGroup(info.packedPosition);
  //取得子菜单项的索引
  int child=ExpandableListView.getPackedPositionChild(info.packedPosition);
  Toast.makeText(MainActivity.this, "type="+type+",group="+group+",child"+child,
    Toast.LENGTH_SHORT).show();
 }    
}

结果


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值