效果图



public class TestExpandableListView extends Activity {ExpandableListView mView;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.expandable_listview);mView = (ExpandableListView) findViewById(R.id.el_list);mView.setAdapter(new MyAdapter());}class MyAdapter extends BaseExpandableListAdapter { //设置组视图的图片//        int[] logos = new int[] { R.drawable.wei, R.drawable.shu,R.drawable.wu};        //设置组视图的显示文字        private String[] generalsTypes = new String[] { "魏", "蜀", "吴" };        //子视图显示文字        private String[][] generals = new String[][] {                { "夏侯惇", "甄姬", "许褚", "郭嘉", "司马懿", "杨修" },                { "马超", "张飞", "刘备", "诸葛亮", "黄月英", "赵云" },                { "吕蒙", "陆逊", "孙权", "周瑜", "孙尚香" }        };                //子视图图片//        public int[][] generallogos = new int[][] {//                { R.drawable.xiahoudun, R.drawable.zhenji,//                        R.drawable.xuchu, R.drawable.guojia,//                        R.drawable.simayi, R.drawable.yangxiu },//                { R.drawable.machao, R.drawable.zhangfei,//                        R.drawable.liubei, R.drawable.zhugeliang,//                        R.drawable.huangyueying, R.drawable.zhaoyun },//                { R.drawable.lvmeng, R.drawable.luxun, R.drawable.sunquan,//                        R.drawable.zhouyu, R.drawable.sunshangxiang } };        //自己定义一个获得textview的方法        TextView getTextView() {            AbsListView.LayoutParams lp = new AbsListView.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, 100);            TextView textView = new TextView(TestExpandableListView.this);            textView.setLayoutParams(lp);            textView.setGravity(Gravity.CENTER_VERTICAL);            textView.setPadding(36, 0, 0, 0);            textView.setTextSize(20);            textView.setTextColor(Color.BLACK);            return textView;        }@Overridepublic int getGroupCount() {return generalsTypes.length;}@Overridepublic int getChildrenCount(int groupPosition) {return generals[groupPosition].length;}@Overridepublic Object getGroup(int groupPosition) {return generalsTypes[groupPosition];}@Overridepublic Object getChild(int groupPosition, int childPosition) {return generals[groupPosition][childPosition];}@Overridepublic long getGroupId(int groupPosition) {return groupPosition;}@Overridepublic long getChildId(int groupPosition, int childPosition) {return childPosition;}@Overridepublic boolean hasStableIds() {return true;}@Overridepublic View getGroupView(int groupPosition, boolean isExpanded,View convertView, ViewGroup parent) { LinearLayout ll = new LinearLayout(TestExpandableListView.this);             ll.setOrientation(0);//             ImageView logo = new ImageView(ExpandableList.this);//             logo.setImageResource(logos[groupPosition]);//             logo.setPadding(50, 0, 0, 0);//             ll.addView(logo);             TextView textView = getTextView();             textView.setTextColor(Color.BLUE);             textView.setText(getGroup(groupPosition).toString());             ll.addView(textView);             ll.setPadding(100, 10, 10, 10);             return ll;}@Overridepublic View getChildView(int groupPosition, int childPosition,boolean isLastChild, View convertView, ViewGroup parent) { LinearLayout ll = new LinearLayout(TestExpandableListView.this);             ll.setOrientation(0);//             ImageView generallogo = new ImageView(TestExpandableListView.this);//             generallogo.setImageResource(generallogos[groupPosition][childPosition]);//             ll.addView(generallogo);             TextView textView = getTextView();             textView.setText(getChild(groupPosition, childPosition).toString());             ll.addView(textView);             return ll;}@Overridepublic boolean isChildSelectable(int groupPosition, int childPosition) {return true;}}}

expandable_listview.xml

<?xml version="1.0" encoding="utf-8"?>           


一些方法说明:

1.  ExpandableListAdapter 

     group和child都有getView方法,即可使用不同的布局,ViewHolder等

     在getView方法的,groupPosition和childPosition 即在两个集合中的位置

2. ExpandableListView 

    getFirstVisiblePosition();
    getLastVisiblePosition();   //返回屏幕最后可见item的位置: 只计算可见view的位置(不含被group 收缩的子view)

   getPackedPositionGroup(getExpandableListPosition(getFirstVisiblePosition()); //返回 首可见项所在group位置

   setOnChildClickListener()   child点击
   setOnGroupClickListener()  group点击

更多相关文章

  1. android抓log方法
  2. android 登陆右上角 的关闭按钮的使用方法
  3. Android AESUtils 工具类与调用方法
  4. sqlit导入外部数据库查找数据方法
  5. Android NDK打印log到logcat的方法
  6. Android获取系统时间的多种方法
  7. Android根据经纬度获取位置信息
  8. Android链式方法显示Dialog

随机推荐

  1. 前端html基础知识之图片/链接/列表和表单
  2. Parrot Linux系统安装
  3. 阿里云Epel镜像的安装方法
  4. css 元素样式来源,基本选择器,上下文选择器
  5. MongoDB 镜像配置方法
  6. centos7安装pip并配置国内阿里镜像源
  7. 在小城市生活的程序员你还好么?
  8. 阿里云mysql镜像的安装方法
  9. 阿里云镜像Centos安装PHP
  10. 使用VM虚拟机安装CentOS-stream系统