android - 在 android 的嵌套 ExpandableListView 中没有子项可见

标签 android expandablelistview

我正在尝试创建一个嵌套的可扩展列表(3 层)。但是使用以下代码看不到子类别列表(第 3 层)。 getChildCount > 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;
    }
}

最佳答案

在您的 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);
    }
}

关于android - 在 android 的嵌套 ExpandableListView 中没有子项可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40618120/

相关文章:

android - 如何仅将相机设置为单色(黑白)图像?

android - 单击子布局中的元素时更改可扩展列表 subview 的背景

java - 可扩展 ListView 未扩展

Android - ExpandableListView 不显示任何内容

Android TextView 不在 ListView 中做换行(多行文本)

android - 在没有 MediaPlayer 的情况下使用 RemoteControlClient

android - 在 fragment 中注册和注销 BroadcastReceiver

android - 我可以在 Scala 中编写一些代码(使用 Android Studio)吗?

Android ExpandableListView 组位置始终为 0

android - 在屏幕底部添加图标