【问题标题】:No child is visible in nested ExpandableListView in android在 android 的嵌套 ExpandableListView 中看不到孩子
【发布时间】:2016-11-15 19:26:38
【问题描述】:

我正在尝试创建一个嵌套的expandable list(3 层)。但是使用以下代码不可见子类别列表(第 3 层)。 getChildCount is > 1getChildview 从未调用过SecondLevelAdapter

-> menu
    -> Category
        -> sub category

数据结构:

class Menu{
    List<Category> categories;
}

class Category{
    List<SubCategory> subcategories;
}

public class ParentLevelAdapter extends BaseExpandableListAdapter {
    private final Context mContext;
    private final List<NavigationMenu> mListDataHeader;

    public ParentLevelAdapter(Context mContext, List<NavigationMenu> mListDataHeader) {
        this.mContext = mContext;
        this.mListDataHeader = mListDataHeader;
    }

    @Override
    public NavCategory getChild(int groupPosition, int childPosition) {
        return mListDataHeader.get(groupPosition).categories.get(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) {
        final ExpandableListView secondLevelExpListView = new ExpandableListView(this.mContext);
        secondLevelExpListView.setAdapter(new SecondLevelAdapter(this.mContext, getChild(groupPosition,childPosition)));
        return secondLevelExpListView;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return mListDataHeader.get(groupPosition).categories.size();
    }

    @Override
    public NavigationMenu getGroup(int groupPosition) {
        return this.mListDataHeader.get(groupPosition);
    }

    @Override
    public int getGroupCount() {
        return this.mListDataHeader.size();
    }

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

    class ViewHolder{
        ImageView icon;
        TextView name;
    }
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
                             View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            LayoutInflater layoutInflater = (LayoutInflater) this.mContext
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = layoutInflater.inflate(R.layout.nav_menu_item_view, parent, false);
            holder = new ViewHolder();
            holder.icon = (ImageView) convertView.findViewById(R.id.icon);
            holder.name = (TextView) convertView.findViewById(R.id.menu_name);
            convertView.setTag(holder);
        }
        else{
            holder = (ViewHolder) convertView.getTag();
        }
        holder.name.setText(getGroup(groupPosition).name);
        holder.icon.setImageResource(getGroup(groupPosition).iconResoluteID);
        return convertView;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
}

public class SecondLevelAdapter extends BaseExpandableListAdapter {
    private final Context mContext;
    private final List<NavSubcategory> mListDataHeader;
    private final NavCategory navCategory;

    public SecondLevelAdapter(Context mContext, NavCategory navCategory) {
        this.mContext = mContext;
        this.mListDataHeader = navCategory.sub_categories;
        this.navCategory = navCategory;
    }

    @Override
    public NavSubcategory getChild(int groupPosition, int childPosition) { //ignore group
        return mListDataHeader.get(childPosition);
    }

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

    @Override
    public boolean hasStableIds() {
        return false;
    }

    class ChildViewHolder{
        TextView name;
    }

    class GroupViewHolder{
        TextView name;
    }


    @Override
    public View getChildView(int groupPosition, int childPosition,
                             boolean isLastChild, View convertView, ViewGroup parent) {
        ChildViewHolder holder;
        if (convertView == null) {
            LayoutInflater layoutInflater = (LayoutInflater) this.mContext
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = layoutInflater.inflate(R.layout.nav_menu_second_item, parent, false);
            holder = new ChildViewHolder();
            holder.name = (TextView) convertView.findViewById(R.id.menu_name);
            convertView.setTag(holder);
        }
        else{
            holder = (ChildViewHolder) convertView.getTag();
        }
        holder.name.setText(getChild(groupPosition,childPosition).sub_category_name);
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return mListDataHeader.size();
    }

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

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

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

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
                             View convertView, ViewGroup parent) {

        GroupViewHolder groupViewHolder;
        if (convertView == null) {
            LayoutInflater layoutInflater = (LayoutInflater) this.mContext
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = layoutInflater.inflate(R.layout.nav_menu_second_group_item, parent, false);
            groupViewHolder = new GroupViewHolder();
            groupViewHolder.name = (TextView) convertView.findViewById(R.id.menu_name);
            convertView.setTag(groupViewHolder);
        }
        else{
            groupViewHolder = (GroupViewHolder) convertView.getTag();
        }
        groupViewHolder.name.setText(getGroup(groupPosition).category_name);
        return convertView;
    }
}

【问题讨论】:

    标签: android expandablelistview


    【解决方案1】:

    在您的 ParentLevelAdapter 的 getChildView 中替换

    final ExpandableListView secondLevelExpListView = new ExpandableListView(this.mContext);
    

    final CustomExpandableListView customExpandableListView = new CustomExpandableListView(this.mContext);
    

    CustomExpandableListView 在哪里:

    public class CustomExpandableListView extends ExpandableListView {
    
        Context context;
    
        public CustomExpandableListView(Context _context)  {
            super(_context);
            context = _context;
        }
    
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)  {
            DisplayMetrics metrics = context.getResources().getDisplayMetrics();
            int screenWidth = (int)(metrics.widthPixels); //or whatever you need here for width of the row
    
            widthMeasureSpec = MeasureSpec.makeMeasureSpec(screenWidth, MeasureSpec.AT_MOST);
            heightMeasureSpec = MeasureSpec.makeMeasureSpec(20000, MeasureSpec.AT_MOST);
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        }
    }
    

    【讨论】:

    • 谢谢 alice_silver_man!你能解释一下为什么没有调用 getChildView 吗?没有高度时不会调用吗?
    猜你喜欢
    • 2014-07-23
    • 2012-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-02
    相关资源
    最近更新 更多