技术与热点应用 您所在的位置:电子展览网 > 行业资讯 > 技术与热点应用 > 手机设计与制造

Android ExpandableListActivity 使用




关键词:

  ExpandableListActivity

  [功能]

  sdk 也有关于ExpandableListActivity 代码例子 路径如下:

  写道

  Android-sdk-windows-1.5_r2platformsandroid-1.5samplesApiDemossrccomexampleandroidapisview

  但是sample例子所使用的数据源是静态的 即不能实时变化 使得其实用性大打折扣 现在以动态数据为例

  [代码 步骤]

  1. 规划数据结构用于存放 Group 及其 子数据 Child

  Java代码

  //存放 Groop 数据

  public List group;

  //存放 Child 数据 以其所属于的Group细分

  public List> child;

  //存放 Groop 数据

  public List group;

  //存放 Child 数据 以其所属于的Group细分

  public List> child;

  2. 定义 class ExpandInfoAdapter extends BaseExpandableListAdapter

  Java代码

  public class ExpandInfoAdapter extends BaseExpandableListAdapter {

  @Override

  public Object getChild(int arg0, int arg1) {

  // TODO Auto-generated method stub

  return null;

  }

  @Override

  public long getChildId(int arg0, int arg1) {

  // TODO Auto-generated method stub

  return 0;

  }

  @Override

  public View getChildView(int arg0, int arg1, boolean arg2, View arg3,

  ViewGroup arg4) {

  // TODO Auto-generated method stub

  return null;

  }

  @Override

  public int getChildrenCount(int arg0) {

  // TODO Auto-generated method stub

  return 0;

  }

  @Override

  public Object getGroup(int arg0) {

  // TODO Auto-generated method stub

  return null;

  }

  @Override

  public int getGroupCount() {

  // TODO Auto-generated method stub

  return 0;

第1页 第2页 第3页 第4页 第5页 第6页 第7页 

  ExpandableListActivity

  [功能]

  sdk 也有关于ExpandableListActivity 代码例子 路径如下:

  写道

  android-sdk-windows-1.5_r2platformsandroid-1.5samplesApiDemossrccomexampleandroidapisview

  但是sample例子所使用的数据源是静态的 即不能实时变化 使得其实用性大打折扣 现在以动态数据为例

  [代码 步骤]

  1. 规划数据结构用于存放 Group 及其 子数据 Child

  Java代码

  //存放 Groop 数据

  public List group;

  //存放 Child 数据 以其所属于的Group细分

  public List> child;

  //存放 Groop 数据

  public List group;

  //存放 Child 数据 以其所属于的Group细分

  public List> child;

  2. 定义 class ExpandInfoAdapter extends BaseExpandableListAdapter

  Java代码

  public class ExpandInfoAdapter extends BaseExpandableListAdapter {

  @Override

  public Object getChild(int arg0, int arg1) {

  // TODO Auto-generated method stub

  return null;

  }

  @Override

  public long getChildId(int arg0, int arg1) {

  // TODO Auto-generated method stub

  return 0;

  }

  @Override

  public View getChildView(int arg0, int arg1, boolean arg2, View arg3,

  ViewGroup arg4) {

  // TODO Auto-generated method stub

  return null;

  }

  @Override

  public int getChildrenCount(int arg0) {

  // TODO Auto-generated method stub

  return 0;

  }

  @Override

  public Object getGroup(int arg0) {

  // TODO Auto-generated method stub

  return null;

  }

  @Override

  public int getGroupCount() {

  // TODO Auto-generated method stub

  return 0;

第1页 第2页 第3页 第4页 第5页 第6页 第7页 

  接上页

  }

  @Override

  public long getGroupId(int arg0) {

  // TODO Auto-generated method stub

  return 0;

  }

  @Override

  public View getGroupView(int groupPosition, boolean isExpanded,

  View convertView, ViewGroup parent) {

  // TODO Auto-generated method stub

  return null;

  }

  @Override

  public boolean hasStableIds() {

  // TODO Auto-generated method stub

  return false;

  }

  @Override

  public boolean isChildSelectable(int groupPosition, int childPosition) {

  // TODO Auto-generated method stub

  return false;

  }

  }

  public class ExpandInfoAdapter extends BaseExpandableListAdapter {

  @Override

  public Object getChild(int arg0, int arg1) {

  // TODO Auto-generated method stub

  return null;

  }

  @Override

  public long getChildId(int arg0, int arg1) {

  // TODO Auto-generated method stub

  return 0;

  }

  @Override

  public View getChildView(int arg0, int arg1, boolean arg2, View arg3,

  ViewGroup arg4) {

  // TODO Auto-generated method stub

  return null;

  }

  @Override

  public int getChildrenCount(int arg0) {

  // TODO Auto-generated method stub

  return 0;

  }

  @Override

  public Object getGroup(int arg0) {

  // TODO Auto-generated method stub

  return null;

  }

  @Override

  public int getGroupCount() {

  // TODO Auto-generated method stub

  return 0;

  }

  @Override

  public long getGroupId(int arg0) {

  // TODO Auto-generated method stub

  return 0;

  }

  @Override

第1页 第2页 第3页 第4页 第5页 第6页 第7页 

  接上页

  public View getGroupView(int groupPosition, boolean isExpanded,

  View convertView, ViewGroup parent) {

  // TODO Auto-generated method stub

  return null;

  }

  @Override

  public boolean hasStableIds() {

  // TODO Auto-generated method stub

  return false;

  }

  @Override

  public boolean isChildSelectable(int groupPosition, int childPosition) {

  // TODO Auto-generated method stub

  return false;

  }

  }

  3. 填充ExpandInfoAdapter 的函数

  * 完成与 Child 功能相关的函数列表

  写道

  public Object getChild(int arg0, int arg1)

  public long getChildId(int arg0, int arg1)

  public View getChildView(int arg0, int arg1, boolean arg2, View arg3,

  ViewGroup arg4)

  public int getChildrenCount(int arg0)

  其实现如下:

  Java代码

  @Override

  public Object getChild(int arg0, int arg1) {

  // TODO Auto-generated method stub

  return child.get(arg0).get(arg1);

  }

  @Override

  public long getChildId(int arg0, int arg1) {

  // TODO Auto-generated method stub

  return arg1;

  }

  @Override

  public int getChildrenCount(int arg0) {

  // TODO Auto-generated method stub

  return child.get(arg0).size();

  }

  @Override

  public View getChildView(int arg0, int arg1, boolean arg2, View arg3,

  ViewGroup arg4) {

  // TODO Auto-generated method stub

  return getChildViewStub(child.get(arg0).get(arg1).toString());

  }

  public TextView getChildViewStub(String s) {

  // Layout parameters for the ExpandableListView

  AbsListView.LayoutParams lp = new AbsListView.LayoutParams(

  ViewGroup.LayoutParams.FILL_PARENT, 64);

第1页 第2页 第3页 第4页 第5页 第6页 第7页 

  接上页

  TextView text = new TextView(activity);

  text.setLayoutParams(lp);

  text.setTextSize(20);

  text.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);

  text.setPadding(36, 0, 0, 0);

  text.setText(s);

  return text;

  }

  @Override

  public Object getChild(int arg0, int arg1) {

  // TODO Auto-generated method stub

  return child.get(arg0).get(arg1);

  }

  @Override

  public long getChildId(int arg0, int arg1) {

  // TODO Auto-generated method stub

  return arg1;

  }

  @Override

  public int getChildrenCount(int arg0) {

  // TODO Auto-generated method stub

  return child.get(arg0).size();

  }

  @Override

  public View getChildView(int arg0, int arg1, boolean arg2, View arg3,

  ViewGroup arg4) {

  // TODO Auto-generated method stub

  return getChildViewStub(child.get(arg0).get(arg1).toString());

  }

  public TextView getChildViewStub(String s) {

  // Layout parameters for the ExpandableListView

  AbsListView.LayoutParams lp = new AbsListView.LayoutParams(

  ViewGroup.LayoutParams.FILL_PARENT, 64);

  TextView text = new TextView(activity);

  text.setLayoutParams(lp);

  text.setTextSize(20);

  text.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);

  text.setPadding(36, 0, 0, 0);

  text.setText(s);

  return text;

  }

  * 再完成与 Group 有关的函数列表

  写道

  public Object getGroup(int arg0)

  public int getGroupCount()

  public long getGroupId(int arg0)

  public View getGroupView(int arg0, boolean arg1, View arg2,

  ViewGroup arg3)

  其实现如下:

  Java代码

  @Override

第1页 第2页 第3页 第4页 第5页 第6页 第7页 

  接上页

  public int getGroupCount() {

  // TODO Auto-generated method stub

  return group.size();

  }

  @Override

  public long getGroupId(int arg0) {

  // TODO Auto-generated method stub

  return arg0;

  }

  @Override

  public View getGroupView(int arg0, boolean arg1, View arg2,

  ViewGroup arg3) {

  // TODO Auto-generated method stub

  return getGroupViewStub(getGroup(arg0).toString());

  }

  public TextView getGroupViewStub(String s) {

  // Layout parameters for the ExpandableListView

  AbsListView.LayoutParams lp = new AbsListView.LayoutParams(

  ViewGroup.LayoutParams.FILL_PARENT, 64);

  TextView text = new TextView(activity);

  text.setLayoutParams(lp);

  text.setTextSize(20);

  text.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);

  text.setPadding(36, 0, 0, 0);

  text.setText(s);

  return text;

  }

  @Override

  public int getGroupCount() {

  // TODO Auto-generated method stub

  return group.size();

  }

  @Override

  public long getGroupId(int arg0) {

  // TODO Auto-generated method stub

  return arg0;

  }

  @Override

  public View getGroupView(int arg0, boolean arg1, View arg2,

  ViewGroup arg3) {

  // TODO Auto-generated method stub

  return getGroupViewStub(getGroup(arg0).toString());

  }

  public TextView getGroupViewStub(String s) {

  // Layout parameters for the ExpandableListView

  AbsListView.LayoutParams lp = new AbsListView.LayoutParams(

  ViewGroup.LayoutParams.FILL_PARENT, 64);

  TextView text = new TextView(activity);

  text.setLayoutParams(lp);

  text.setTextSize(20);

第1页 第2页 第3页 第4页 第5页 第6页 第7页 

  接上页

  text.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);

  text.setPadding(36, 0, 0, 0);

  text.setText(s);

  return text;

  }

  4. BaseExpandableListAdapter 除了 Group Child 外 还有一些别的函数 如:

  *

  Java代码

  public void onGroupExpanded(int groupPosition){

  }

  public void onGroupCollapsed(int groupPosition){

  }

  public void onGroupExpanded(int groupPosition){

  }

  public void onGroupCollapsed(int groupPosition){

  }

  写道

  二者属于回调函数 当ExpandableListActivity中的Group 被展开/收拢 就会回调上面的函数

  *

  Java代码

  public boolean isChildSelectable(int arg0, int arg1) {

  // TODO Auto-generated method stub

  return true;

  }

  public boolean isChildSelectable(int arg0, int arg1) {

  // TODO Auto-generated method stub

  return true;

  }

  写道

  用于设定 Child 是否可以选择 获取 selected

  5. 插入数据

  Java代码

  public void addItemByValue(String n,String c1,String c2){

  group.add(n);

  List item = new ArrayList();

  item.add(NAME+n);

  item.add(PHONE+c1);

  item.add(SEX+c2);

  child.add(item);

  }

  public void addItemByValue(String n,String c1,String c2){

  group.add(n);

  List item = new ArrayList();

  item.add(NAME+n);

  item.add(PHONE+c1);

  item.add(SEX+c2);

  child.add(item);

  }

  本例的数据为:

  Java代码

  addItemByValue("Griffin","051782214","man");

  addItemByValue("Billy","110","mal");

  addItemByValue("Kindy","132","femal");

  addItemByValue("Patric","13321234562","femal");

第1页 第2页 第3页 第4页 第5页 第6页 第7页 

  接上页

  addItemByValue("Griffin","051782214","man");

  addItemByValue("Billy","110","mal");

  addItemByValue("Kindy","132","femal");

  addItemByValue("Patric","13321234562","femal");

  6. emulator 运行截图:

  * Group 的截图:

  * Griffin 选项展开截图:

  * Patric 选项展开截图:

第1页 第2页 第3页 第4页 第5页 第6页 第7页 

分享到:
收藏 打印
相关新闻
  • 没有相关信息!