zoukankan      html  css  js  c++  java
  • Android ExpandableGridView的实现

    近期在做项目的时候碰到了这样一个布局

     

     

     

    在android中有种实现折叠list方式是ExpandableListView  但是官方没有ExpandableGridView 

    那么怎么样用ExpandableListView来实现一个ExpandableGridView呢

     

    大概的原理是:在每个ExpandableListView 中显示一行,用这行来加载一个GridView

    口说无凭,贴上代码

    1.加载一个ExpandableListView

    private SimpleDataAdapter simpleDataAdapter;
     private ExpandableListView mexpandableListview;
    
    
    
    
            mexpandableListview = (ExpandableListView) findViewById(R.id.expandablelist);
            mSuperAdapter = new SuperveseDataExpandableAdapter(this,mSuperveseData);
            mexpandableListview.setAdapter(mSuperAdapter);
            mexpandableListview.expandGroup(0);  

      2.重写BaseExpandableListAdapter

    重写BaseExpandableListAdapter,主要是重写getChildView方法 加载一个GridView 动态的计算GridView 高度

        @Override
        public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = layoutInflater.inflate(R.layout.view, null);
            }
            CustomGridView gridView = (CustomGridView) convertView .findViewById(R.id.GridView_toolbar);
    
            gridView.setNumColumns(2);
            gridView.setHorizontalSpacing(10);
            gridView.setGravity(Gravity.CENTER);
            GridAdapter adapter = new GridAdapter(context, superveseDatas.get(groupPosition).controlDatas);
            gridView.setAdapter(adapter);// Adapter
    
            int totalHeight = 0;
           for (int size = 0; size < adapter.getCount(); size++) {
                RelativeLayout relativeLayout = (RelativeLayout) adapter.getView(size, null, gridView);
                TextView textView = (TextView) relativeLayout.getChildAt(0);
                textView.measure(0, 0);
                totalHeight += textView.getMeasuredHeight()*2;
                if(size == adapter.getCount() -1){
                    if (size%2 == 0 ){
                        totalHeight += textView.getMeasuredHeight()*2;
                    }
                }
            }
            gridView.SetHeight(totalHeight);
    
            return convertView;
        }
    

      另外一个地方也需要注意下:

    getChildrenCount返回1 因为用一个Item 来加载GridView
     @Override
        public int getChildrenCount(int groupPosition) {
            return  1;
        }
    

      

     

    最后实现的效果如下

     

     

     

  • 相关阅读:
    Python基础笔记
    Oracle PL/SQL学习之Hello World(0)
    编程开发之--Oracle数据库--存储过程在out参数中使用光标(3)
    编程开发之--Oracle数据库--存储过程和存储函数(2)
    编程开发之--Oracle数据库--存储过程和存储函数(1)
    火车票售票系统
    MySQL 字段内容区分大小写
    json_encode($b, JSON_FORCE_OBJECT) 可以强制转换成对象
    Sublime Text3配置
    springboot-配置多个数据源
  • 原文地址:https://www.cnblogs.com/huwei0814/p/4461584.html
Copyright © 2011-2022 走看看