Android Open Source - ExpandableListDemoApp Drawer Expandable Adapter






From Project

Back to project page ExpandableListDemoApp.

License

The source code is released under:

MIT License

If you think the Android project ExpandableListDemoApp listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package de.frost.david.android.adapters;
//from w w w .  j  a v  a2 s .  co m
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import java.util.List;
import java.util.Map;

/**
 * ExpandableListDemoApp
 * <p/>
 * Created by david on 18.11.14.
 *
 * DrawerExpandableAdapter is a slightly modified version of the SimpleExpandableListAdapter.
 *
 */
public class DrawerExpandableAdapter extends BaseExpandableListAdapter {
    private List<? extends Map<String, ?>> mGroupData;
    private int mExpandedGroupLayout;
    private int mCollapsedGroupLayout;
    private int mNoChildGroupLayout;
    private String[] mGroupFrom;
    private int[] mGroupTo;

    private List<? extends List<? extends Map<String, ?>>> mChildData;
    private int mChildLayout;
    private int mLastChildLayout;
    private String[] mChildFrom;
    private int[] mChildTo;

    private LayoutInflater mInflater;

    public DrawerExpandableAdapter(Context context,
                                   List<? extends Map<String, ?>> groupData, int groupLayout,
                                   String[] groupFrom, int[] groupTo,
                                   List<? extends List<? extends Map<String, ?>>> childData,
                                   int childLayout, String[] childFrom, int[] childTo) {
        this(context, groupData, groupLayout, groupLayout, groupFrom, groupTo, childData,
                childLayout, childLayout, childFrom, childTo);
    }

    public DrawerExpandableAdapter(Context context,
                                   List<? extends Map<String, ?>> groupData, int expandedGroupLayout,
                                   int collapsedGroupLayout, String[] groupFrom, int[] groupTo,
                                   List<? extends List<? extends Map<String, ?>>> childData,
                                   int childLayout, String[] childFrom, int[] childTo) {
        this(context, groupData, expandedGroupLayout, collapsedGroupLayout,
                groupFrom, groupTo, childData, childLayout, childLayout,
                childFrom, childTo);
    }

    public DrawerExpandableAdapter(Context context,
                                   List<? extends Map<String, ?>> groupData, int expandedGroupLayout,
                                   int collapsedGroupLayout, int noChildGroupLayout, String[] groupFrom, int[] groupTo,
                                   List<? extends List<? extends Map<String, ?>>> childData,
                                   int childLayout, String[] childFrom, int[] childTo) {
        this(context, groupData, expandedGroupLayout, collapsedGroupLayout,
                groupFrom, groupTo, childData, childLayout, childLayout,
                childFrom, childTo);

        mNoChildGroupLayout = noChildGroupLayout;
    }

    public DrawerExpandableAdapter(Context context,
                                   List<? extends Map<String, ?>> groupData, int expandedGroupLayout,
                                   int collapsedGroupLayout, String[] groupFrom, int[] groupTo,
                                   List<? extends List<? extends Map<String, ?>>> childData,
                                   int childLayout, int lastChildLayout, String[] childFrom,
                                   int[] childTo) {
        mGroupData = groupData;
        mExpandedGroupLayout = expandedGroupLayout;
        mCollapsedGroupLayout = collapsedGroupLayout;
        mGroupFrom = groupFrom;
        mGroupTo = groupTo;

        mChildData = childData;
        mChildLayout = childLayout;
        mLastChildLayout = lastChildLayout;
        mChildFrom = childFrom;
        mChildTo = childTo;

        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public Object getChild(int groupPosition, int childPosition) {
        return mChildData.get(groupPosition).get(childPosition);
    }

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

    public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
                             View convertView, ViewGroup parent) {
        View v;
        if (convertView == null) {
            v = newChildView(isLastChild, parent);
        } else {
            v = convertView;
        }
        bindView(v, mChildData.get(groupPosition).get(childPosition), mChildFrom, mChildTo);
        return v;
    }

    /**
     * Instantiates a new View for a child.
     *
     * @param isLastChild Whether the child is the last child within its group.
     * @param parent      The eventual parent of this new View.
     * @return A new child View
     */
    public View newChildView(boolean isLastChild, ViewGroup parent) {
        return mInflater.inflate((isLastChild) ? mLastChildLayout : mChildLayout, parent, false);
    }

    private void bindView(View view, Map<String, ?> data, String[] from, int[] to) {
        int len = to.length;

        for (int i = 0; i < len; i++) {
            TextView v = (TextView) view.findViewById(to[i]);
            if (v != null) {
                v.setText((String) data.get(from[i]));
            }
        }
    }

    public int getChildrenCount(int groupPosition) {
        return mChildData.get(groupPosition).size();
    }

    public Object getGroup(int groupPosition) {
        return mGroupData.get(groupPosition);
    }

    public int getGroupCount() {
        return mGroupData.size();
    }

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

    public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
                             ViewGroup parent) {
        View v;
        /*
         * This part is different from the SimpleExpandableListAdapter
         *
         * I removed the reusing of the views because I want a view with
         * a different layout for open or closed group items.
         *
         */

//        if (convertView == null) {
            v = newGroupView(isExpanded, parent, groupPosition);
//        } else {
//            v = convertView;
//        }
        bindView(v, mGroupData.get(groupPosition), mGroupFrom, mGroupTo);
        return v;
    }

    /**
     * Instantiates a new View for a group.
     *
     * @param isExpanded    Whether the group is currently expanded.
     * @param parent        The eventual parent of this new View.
     * @param groupPosition
     * @return A new group View
     */
    public View newGroupView(boolean isExpanded, ViewGroup parent, int groupPosition) {

        int resource = (isExpanded) ? mExpandedGroupLayout : mCollapsedGroupLayout;

        /*
         * This part is different from the SimpleExpandableListAdapter
         *
         * Groups without any child elements will get a different layout than groups
         * with child elements.
         * This way the user knows what item will expand the list and what item
         * will do some sort of action.
         *
         */
        if (mChildData.get(groupPosition).size() == 0) resource = mNoChildGroupLayout;

        return mInflater.inflate(
                resource,
                parent,
                false);
    }

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

    public boolean hasStableIds() {
        return true;
    }
}




Java Source Code List

de.frost.david.android.ApplicationTest.java
de.frost.david.android.MyActivity.java
de.frost.david.android.adapters.DrawerExpandableAdapter.java
de.frost.david.android.fragments.ItemListFragmentFragment.java
de.frost.david.android.fragments.NavigationDrawerFragment.java
de.frost.david.android.model.Category.java
de.frost.david.android.model.SubCategory.java