多级可扩展列表视图未扩展到上一个孩子

时间:2017-10-23 12:08:48

标签: android expandablelistview

我有5个级别的多级可扩展列表视图。所有孩子都可以看到最多3个级别,后来在第4级我缺少一些项目,在第5级我只能看到1个项目。每个级别有4个元素,并且是固定的。似乎expandablelist有所有项目,但身高有问题。因此,我必须更正此高度设置才能使显示正确。我已经将MeasureSpec模式更改为2300的值,一切都很完美但看起来不太好,因为当第3级和第4级没有扩展时,有很多可用空间。

我已将expandablelistview高度设置为match_parent

更新:包含代码信息

这是3级,1级,2级的脚本是类似的



private class Level3 extends BaseExpandableListAdapter {
        ViewHolder holder;
        LEVELS[] level;
        pratyantarDasha(LEVELS[] level){
            this.level = new planetData[level.length];
            System.arraycopy(level,0,this.level,0,level.length);
        }
        @Override
        public Object getChild(int groupPosition, int childPosition) {
            return childPosition;
        }

        @Override
        public long getChildId(int groupPosition, int childPosition) {
            return childPosition;
        }

        @Override
        public View getChildView(int groupPosition, int childPosition,
                                 boolean isLastChild, View convertView, ViewGroup parent) {
            int height=groupPosition*72;
            final CustExpListview level4 = new CustExpListview(ctx);
            level4.setAdapter(new Level4(level[groupPosition].nextLevel));
            level4.setGroupIndicator(ContextCompat.getDrawable(ctx,R.drawable.expand_collapse));
            final int[] lastExpPosition = {-1};
            level4.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
                @Override
                public void onGroupExpand(int groupPosition) {
                    if (lastExpPosition[0] != -1 && groupPosition != lastExpPosition[0]) {
                        level4.collapseGroup(lastExpPosition[0]);
                    }
                    lastExpPosition[0] = groupPosition;
                }
            });
             level4.setPadding((int)ctx.getResources().getDimension(R.dimen.dp2)*5,(int)ctx.getResources().getDimension(R.dimen.dp2),(int)ctx.getResources().getDimension(R.dimen.dp2),(int)ctx.getResources().getDimension(R.dimen.dp2));
            return level4;
        }

        @Override
        public int getChildrenCount(int groupPosition) {
            return 1;
        }

        @Override
        public Object getGroup(int groupPosition) {
            return groupPosition;
        }

        @Override
        public int getGroupCount() {
            return 4;
        }

        @Override
        public long getGroupId(int groupPosition) {
            return groupPosition;
        }

        @Override
        public View getGroupView(int groupPosition, boolean isExpanded,
                                 View convertView, ViewGroup parent) {
            if(convertView == null ) {
                LayoutInflater inf = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = inf.inflate(R.id.textViewLayout4Exp, parent,false);
                holder = new ViewHolder();
                holder.planet = (TextView) convertView.findViewById(R.id.expTV);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.value.setText(String.valueOf(level[groupPosition].value));
            return convertView;
        }

        @Override
        public boolean hasStableIds() {
            // TODO Auto-generated method stub
            return true;
        }

        @Override
        public boolean isChildSelectable(int groupPosition, int childPosition) {
            // TODO Auto-generated method stub
            return true;
        }

    }




第4级剧本



private class Level4 extends BaseExpandableListAdapter {
        ViewHolder holder;
        LEVELS[] level;
        Level4(LEVELS[] level){
            this.level = new LEVELS[level.length];
            System.arraycopy(level,0,this.level,0,level.length);
        }
        @Override
        public Object getChild(int groupPosition, int childPosition) {
            return childPosition;
        }

        @Override
        public long getChildId(int groupPosition, int childPosition) {
            return childPosition;
        }

        @Override
        public View getChildView(int groupPosition, int childPosition,
                                 boolean isLastChild, View convertView, ViewGroup parent) {
            if(convertView == null ) {
                LayoutInflater inf = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = inf.inflate(R.id.textViewLayout4Exp, parent,false);
                holder = new ViewHolder();
                holder.planet = (TextView) convertView.findViewById(R.id.expTV);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.planet.setText(String.valueOf(level[groupPosition].nextLevel[childPosition].value));
            return convertView;
        }

        @Override
        public int getChildrenCount(int groupPosition) {
            return 4;
        }

        @Override
        public Object getGroup(int groupPosition) {
            return groupPosition;
        }

        @Override
        public int getGroupCount() {
            return 4;
        }

        @Override
        public long getGroupId(int groupPosition) {
            return groupPosition;
        }

        @Override
        public View getGroupView(int groupPosition, boolean isExpanded,
                                 View convertView, ViewGroup parent) {
            if(convertView == null ) {
                LayoutInflater inf = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = inf.inflate(R.id.textViewLayout4Exp, parent,false);
                holder = new ViewHolder();
                holder.value = (TextView) convertView.findViewById(R.id.expTV);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.value.setText(String.valueOf(level[groupPosition].value));
            
            return convertView;
        }

        @Override
        public boolean hasStableIds() {
            // TODO Auto-generated method stub
            return true;
        }

        @Override
        public boolean isChildSelectable(int groupPosition, int childPosition) {
            // TODO Auto-generated method stub
            return true;
        }

    }




这是自定义的expandablelistview类



 public class CustExpListview extends ExpandableListView {
        
        public CustExpListview(Context context) {
            super(context);
        }

        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            widthMeasureSpec = MeasureSpec.makeMeasureSpec(MeasureSpec.getSize(widthMeasureSpec), MeasureSpec.EXACTLY);
            heightMeasureSpec = MeasureSpec.makeMeasureSpec(99999,MeasureSpec.AT_MOST);
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        }
    }




0 个答案:

没有答案