BaseExpandableListAdapter
发布日期:2022-03-15 04:11:21 浏览次数:58 分类:技术文章

本文共 4337 字,大约阅读时间需要 14 分钟。

先看效果~
<ignore_js_op>
1.gif
2011-5-8 08:45 上传
 
也就是BaseExpandableListAdapter、AbsListView类的使用,就不多说了..大牛留情...
就两个类。
ExpandLabel:
  1. package com.yfz;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import android.app.Activity;
  5. import android.os.Bundle;
  6. import android.view.ViewGroup.LayoutParams;
  7. import android.widget.ExpandableListView;
  8. import android.widget.ViewFlipper;
  9. public class ExpandLabel extends Activity {
  10.     /** Called when the activity is first created. */
  11.     @Override
  12.     public void onCreate(Bundle savedInstanceState) {
  13.         super.onCreate(savedInstanceState);
  14.         setContentView(R.layout.main);
  15.        
  16.         // 数据源
  17.         //标题
  18.         List<String> groupArray = new ArrayList<String>();
  19.         //子标题
  20.         List<List<String>> childArray = new ArrayList<List<String>>();
  21.                 groupArray.add("第一章");
  22.                 groupArray.add("第二章");
  23.                 List<String> tempArray01 = new ArrayList<String>();
  24.                 tempArray01.add("第1.1节");
  25.                 tempArray01.add("第1.2节");
  26.                 tempArray01.add("第1.3节");
  27.                
  28.                 List<String> tempArray02 = new ArrayList<String>();
  29.                 tempArray02.add("第2.1节");
  30.                 tempArray02.add("第2.2节");
  31.                 tempArray02.add("第2.3节");
  32.                
  33.                 childArray.add(tempArray01);
  34.                 childArray.add(tempArray02);
  35.                
  36.         ExpandableListView expandableListView = new ExpandableListView(this);
  37.        
  38.         ExpandableAdapter adapter = new ExpandableAdapter(this,groupArray,childArray);
  39.        
  40.                 expandableListView.setAdapter(adapter);
  41.                
  42.                 addContentView(expandableListView, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
  43.     }
  44. }
复制代码

树形菜单类,只要传两个List过来就可以直接使用了。groupArray:表示一级菜单childArray: 表示二级菜单 二级菜单相当于一个二维List,该List中存储的每个元素都是一个List。第一个元素对应一级菜单中第一个菜单的子项List,第二个元素对应一级菜单中第二个菜单的子项List,依次类推。ExpandableAdapter:

  1. package com.yfz;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import android.app.Activity;
  5. import android.view.Gravity;
  6. import android.view.View;
  7. import android.view.ViewGroup;
  8. import android.widget.AbsListView;
  9. import android.widget.BaseExpandableListAdapter;
  10. import android.widget.TextView;
  11. public class ExpandableAdapter extends BaseExpandableListAdapter {
  12.                
  13.         private List<String> groupArray;
  14.         private List<List<String>> childArray;
  15.         private Activity activity;
  16.         public ExpandableAdapter(Activity a,List<String> groupArray,List<List<String>> childArray) {
  17.                 activity = a;
  18.                 this.groupArray = groupArray;
  19.                 this.childArray = childArray;
  20.         }
  21.        
  22.         @Override
  23.         public Object getChild(int groupPosition, int childPosition) {
  24.                 // TODO Auto-generated method stub
  25.                 return childArray.get(groupPosition).get(childPosition);
  26.         }
  27.         @Override
  28.         public long getChildId(int groupPosition, int childPosition) {
  29.                 // TODO Auto-generated method stub
  30.                 return childPosition;
  31.         }
  32.         @Override
  33.         public View getChildView(int groupPosition, int childPosition,
  34.                         boolean isLastChild, View convertView, ViewGroup parent) {
  35.                 // TODO Auto-generated method stub
  36.                 String string = childArray.get(groupPosition).get(childPosition);
  37.                 return getGenericView(string);
  38.         }
  39.         @Override
  40.         public int getChildrenCount(int groupPosition) {
  41.                 // TODO Auto-generated method stub
  42.                 return childArray.get(groupPosition).size();
  43.         }
  44.         @Override
  45.         public Object getGroup(int groupPosition) {
  46.                 // TODO Auto-generated method stub
  47.                 return groupArray.get(groupPosition);
  48.         }
  49.         @Override
  50.         public int getGroupCount() {
  51.                 // TODO Auto-generated method stub
  52.                 return groupArray.size();
  53.         }
  54.         @Override
  55.         public long getGroupId(int groupPosition) {
  56.                 // TODO Auto-generated method stub
  57.                 return groupPosition;
  58.         }
  59.         @Override
  60.         public View getGroupView(int groupPosition, boolean isExpanded,
  61.                         View convertView, ViewGroup parent) {
  62.                 // TODO Auto-generated method stub
  63.                 String string = groupArray.get(groupPosition);
  64.                 return getGenericView(string);
  65.         }
  66.         @Override
  67.         public boolean hasStableIds() {
  68.                 // TODO Auto-generated method stub
  69.                 return false;
  70.         }
  71.         @Override
  72.         public boolean isChildSelectable(int groupPosition, int childPosition) {
  73.                 // TODO Auto-generated method stub
  74.                 return false;
  75.         }
  76.        
  77. /****************************************以下为自定义方法*********************************************/       
  78.         /**
  79.          * Children 's View
  80.          * @param string
  81.          * @return
  82.          */
  83.         public TextView getGenericView(String string) {
  84.                 AbsListView.LayoutParams layoutParams = new AbsListView.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, 64);
  85.                 TextView text = new TextView(activity);
  86.                 text.setLayoutParams(layoutParams);
  87.                 // Center the text vertically
  88.                 text.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
  89.                 // Set the text starting position
  90.                 text.setPadding(36, 0, 0, 0);
  91.                 text.setText(string);
  92.                 return text;
  93.         }
  94. }

转载于:https://www.cnblogs.com/ada-zheng/archive/2012/12/28/2836744.html

转载地址:https://blog.csdn.net/weixin_30536513/article/details/99810305 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:canvas基本用法
下一篇:Log4net 日志使用介绍

发表评论

最新留言

能坚持,总会有不一样的收获!
[***.219.124.196]2024年04月10日 07时30分18秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章