2

So I've just stepped in the territory of ExpandableListView and wish to make a simple List with only one group and 3 children in a DrawerLayout

Here's my Adapter code :

    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;
    }
}

And here's my DrawerShopModel

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

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

I simply follow MultiAutocompleteTextView method to make the model.

And finally here's how I call it in the drawer :

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);
}

The list is not showing at all in the drawer. I feel off about the adapter, can anyone lead me to the right direction in this?

Apurva
  • 7,370
  • 6
  • 35
  • 57
Kevin Murvie
  • 2,240
  • 1
  • 18
  • 38

2 Answers2

0

IF you are trying to get expandable list in drawer layout try this way,this exactly what you looking for

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;
    }
}

For more Check Expandable List in Drawer

Aditya Vyas-Lakhan
  • 12,393
  • 15
  • 55
  • 92
  • What?! So `Map>`, in my case `Map>` is necessary for this?? – Kevin Murvie May 31 '16 at 03:55
  • DrawerShopModel is your pojo class,you can use it. – Aditya Vyas-Lakhan May 31 '16 at 03:56
  • Yes, I mean we have to use `Map` for the list right?? Out of context : I like the name `POJO` btw, POJO JOJO hahahaa – Kevin Murvie May 31 '16 at 04:04
  • Alrighty, be back in about.. 10 mins, after reading the github repo and applying it into my code – Kevin Murvie May 31 '16 at 04:06
  • sure and let me know if you have any issue – Aditya Vyas-Lakhan May 31 '16 at 04:07
  • Managed to show the header only, but not the content.. Weird, an adapter needs item count > 0 to have its content shown right? – Kevin Murvie May 31 '16 at 04:32
  • Btw I just realized, it uses `ExpandableListView` as a drawer right? I am using `Fragment` as a drawer. I managed to show the listview and its contents, but it doesn't resize its layout when it is expanded, it takes space as small as its title.. Why does this happen? – Kevin Murvie May 31 '16 at 06:03
  • It worked, but I got new problem.. :( Your answer led me to the completion of the `ExpandableListView` but sadly, new problem arises, here it is if you wish to see http://stackoverflow.com/questions/37605545/android-nestedscrollview-which-contains-expandablelistview-doesnt-scroll-when/37605908#37605908 :D – Kevin Murvie Jun 03 '16 at 11:11
  • Yes I had just checked there and yep his answer is a good one! I don't feel any performance issue whatsoever.. Happy coding to you too! – Kevin Murvie Jun 03 '16 at 11:24
0

It seems that you are doing everything right but just missing to set the Adapter in your ExpandableListView

In your setupShop() method you will have to set your Adapter like this,

expandableListView.setAdapter(drawerShopAdapter);
Satyen Udeshi
  • 3,175
  • 1
  • 16
  • 25