【问题标题】:Android - ExpandableListView not showing anythingAndroid - ExpandableListView 不显示任何内容
【发布时间】:2016-05-31 03:46:56
【问题描述】:

所以我刚刚踏入ExpandableListView 的领域,并希望在DrawerLayout 中创建一个只有一组和三个孩子的简单列表

这是我的适配器代码:

    public class DrawerShopAdapter extends BaseExpandableListAdapter {
    private Context context;
    private List<String> headerTitle = new ArrayList<>();
    private List<DrawerShopModel> drawerList = new ArrayList<>();

    public DrawerShopAdapter(Context context, List<DrawerShopModel> dataset) {
        this.context = context;
        this.drawerList.addAll(dataset);
        this.headerTitle.add("Toko Saya");
    }

    @Override
    public Object getChild(int groupPosition, int childPosititon) {
        return this.drawerList.get(childPosititon).drawer_txt;
    }

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

    @Override
    public View getChildView(int groupPosition, final int childPosition,
                             boolean isLastChild, View convertView, ViewGroup parent) {

        final String childText = (String) getChild(groupPosition, childPosition);

        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this.context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.drawer_shop_item, null);
        }

        ImageView drawerIcon = (ImageView)convertView.findViewById(R.id.drawer_shop_item_img);
        OpenSansFont drawerTxt = (OpenSansFont) convertView.findViewById(R.id.drawer_shop_item_txt);

        drawerIcon.setImageDrawable(context.getResources().getDrawable(drawerList.get(childPosition).drawer_image));
        drawerTxt.setText(childText);
        return convertView;
    }

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

    @Override
    public Object getGroup(int groupPosition) {
        return this.headerTitle.get(groupPosition);
    }

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

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

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
                             View convertView, ViewGroup parent) {
        String headerTitle = (String) getGroup(groupPosition);
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this.context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.drawer_shop_header, null);
        }

        OpenSansFont listHeader = (OpenSansFont) convertView
                .findViewById(R.id.drawer_shop_header_title);
//        listHeader.setTypeface(null, Typeface.BOLD);
        listHeader.setText(headerTitle);

        return convertView;
    }

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

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

这是我的DrawerShopModel

public class DrawerShopModel {
    public int drawer_image;
    public String drawer_txt;

    @Override
    public String toString() {
        return drawer_txt;
    }
}

我只是按照MultiAutocompleteTextView的方法制作模型。

最后我在抽屉里这样称呼它:

public void setupShop(){
    expandableListView.setVisibility(View.VISIBLE);
    int images[] = { R.drawable.penjualan_gray, R.drawable.daftar_produk, R.drawable.keluhan_gray };
    String[] names = { "Penjualan", "Daftar Produk", "Keluhan"};

    List<DrawerShopModel> drawerShopModels = new ArrayList<>();

    for (int i = 0; i < 3; i++){
        DrawerShopModel drawerShopModel = new DrawerShopModel();
        drawerShopModel.drawer_image = images[i];
        drawerShopModel.drawer_txt = names[i];
        drawerShopModels.add(drawerShopModel);
    }

    drawerShopAdapter = new DrawerShopAdapter(this.getActivity(), drawerShopModels);
expandableListView.setAdapter(drawerShopAdapter);
}

列表根本没有显示在抽屉中。我对适配器感到不舒服,谁能引导我走向正确的方向?

【问题讨论】:

    标签: android expandablelistview


    【解决方案1】:

    如果您想在抽屉布局中获取可扩展列表,请尝试这种方式,这正是您要寻找的

    public class CustomExpandableListAdapter extends BaseExpandableListAdapter {
    
        private Context mContext;
        private List<String> mExpandableListTitle;
        private Map<String, List<String>> mExpandableListDetail;
        private LayoutInflater mLayoutInflater;
    
        public CustomExpandableListAdapter(Context context, List<String> expandableListTitle,
                                           Map<String, List<String>> expandableListDetail) {
            mContext = context;
            mExpandableListTitle = expandableListTitle;
            mExpandableListDetail = expandableListDetail;
            mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }
    
        @Override
        public Object getChild(int listPosition, int expandedListPosition) {
            return mExpandableListDetail.get(mExpandableListTitle.get(listPosition))
                .get(expandedListPosition);
        }
    
        @Override
        public long getChildId(int listPosition, int expandedListPosition) {
            return expandedListPosition;
        }
    
        @Override
        public View getChildView(int listPosition, final int expandedListPosition,
                                 boolean isLastChild, View convertView, ViewGroup parent) {
            final String expandedListText = (String) getChild(listPosition, expandedListPosition);
            if (convertView == null) {
                convertView = mLayoutInflater.inflate(R.layout.list_item, null);
            }
            TextView expandedListTextView = (TextView) convertView
                .findViewById(R.id.expandedListItem);
            expandedListTextView.setText(expandedListText);
            return convertView;
        }
    
        @Override
        public int getChildrenCount(int listPosition) {
            return mExpandableListDetail.get(mExpandableListTitle.get(listPosition))
                .size();
        }
    
        @Override
        public Object getGroup(int listPosition) {
            return mExpandableListTitle.get(listPosition);
        }
    
        @Override
        public int getGroupCount() {
            return mExpandableListTitle.size();
        }
    
        @Override
        public long getGroupId(int listPosition) {
            return listPosition;
        }
    
        @Override
        public View getGroupView(int listPosition, boolean isExpanded,
                                 View convertView, ViewGroup parent) {
            String listTitle = (String) getGroup(listPosition);
            if (convertView == null) {
                convertView = mLayoutInflater.inflate(R.layout.list_group, null);
            }
            TextView listTitleTextView = (TextView) convertView
                .findViewById(R.id.listTitle);
            listTitleTextView.setTypeface(null, Typeface.BOLD);
            listTitleTextView.setText(listTitle);
            return convertView;
        }
    
        @Override
        public boolean hasStableIds() {
            return false;
        }
    
        @Override
        public boolean isChildSelectable(int listPosition, int expandedListPosition) {
            return true;
        }
    }
    

    更多请查看Expandable List in Drawer

    【讨论】:

    • 什么?!所以Map&lt;String, List&lt;String&gt;&gt;,在我的情况下Map&lt;String, List&lt;DrawerShopModel&gt;&gt;是必要的??
    • DrawerShopModel 是你的 pojo 类,你可以使用它。
    • 是的,我的意思是我们必须使用Map 作为列表,对吗?断章取义:我喜欢这个名字POJO btw,POJO JOJO 哈哈哈
    • 好的,大约 10 分钟后回来,在阅读 github 存储库并将其应用到我的代码之后
    • 确定,如果您有任何问题,请告诉我
    【解决方案2】:

    看来你做的一切都是正确的,但只是错过在你的ExpandableListView 中设置Adapter

    在您的 setupShop() 方法中,您必须像这样设置您的 Adapter

    expandableListView.setAdapter(drawerShopAdapter);
    

    【讨论】:

    • 哦,我在 setupShop() 下做了那个;调用,为清楚起见,我现在将其放入 setupShop() 中,呵呵
    • 哦,我比 :D 糟糕,你能发布同样的 xml 文件吗,因为你的代码中的一切似乎都正确
    • 我确实错过了一些东西,我没有将组头放在适配器中,因此没有显示任何内容,将更新我改进的适配器
    猜你喜欢
    • 2019-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多