Android Open Source - AnotherExpandableListView Another Expandable Adapter






From Project

Back to project page AnotherExpandableListView.

License

The source code is released under:

GNU General Public License

If you think the Android project AnotherExpandableListView 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 es.laux;
/*from   w  w w  .j av a 2  s.co m*/
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import es.laux.models.AnotherExpandableChild;
import es.laux.models.AnotherExpandableGroup;

/**
 * Base class to get the ListView. This extends from BaseExpandableListAdapter
 */
public class AnotherExpandableAdapter extends BaseExpandableListAdapter implements Animation.AnimationListener {

    // Arror style
    private int arrow;
    private int resOpenArrow;
    private int resCloseArrow;

    // List of groups
    private List<AnotherExpandableGroup> groups;

    // Context
    private Context context;

    // Is the view animated?
    private boolean animating = false;

    // Animations
    RotateAnimation openAnimation;
    RotateAnimation closeAnimation;

    /**
     * Basic constructor
     *
     * @param context Context of the application
     */
    public AnotherExpandableAdapter(Context context) {

        // Set an empty list
        this(new ArrayList<AnotherExpandableGroup>(), context);

    }

    public void setArrow(int arrow) {
        this.arrow = arrow;
    }



    /**
     * Set an List of groups
     *
     * @param groups  List of groups
     * @param context Context of the application
     */
    public AnotherExpandableAdapter(List<AnotherExpandableGroup> groups, Context context) {

        // Initiate the default arrow
        this(groups, context, Resources.DEFAULT_ARROW);

    }

    /**
     * Set the groups and the arrow style
     *
     * @param groups  List of groups
     * @param context Context of the application
     * @param arrow   type of arrow
     */
    public AnotherExpandableAdapter(List<AnotherExpandableGroup> groups, Context context, int arrow) {

        // Save the groups
        this.groups = groups;

        // Save context
        this.context = context;

        // Save the arrow style
        switch (arrow) {
            case Resources.ARROW_BOLD:
                this.arrow = Resources.ARROW_BOLD;
                this.resOpenArrow = Resources.RES_ARROW_OPEN_BOLD;
                this.resCloseArrow = Resources.RES_ARROW_CLOSE_BOLD;
                break;
            default:
                // Default is thin arrow
                this.arrow = Resources.ARROW_THIN;
                this.resOpenArrow = Resources.RES_ARROW_OPEN_THIN;
                this.resCloseArrow = Resources.RES_ARROW_CLOSE_THIN;
                break;
        }

        // Save the animations
        initAnimations();

    }

    /**
     * Function to init the animations
     */
    public void initAnimations() {

        openAnimation = new RotateAnimation(0, 90, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        openAnimation.setDuration(100);
        openAnimation.setAnimationListener(this);
        openAnimation.setFillAfter(false);

        closeAnimation = new RotateAnimation(0, -90, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        closeAnimation.setDuration(100);
        closeAnimation.setAnimationListener(this);
        closeAnimation.setFillAfter(false);
    }

    /**
     * Function to add an empty group without icon
     */
    public void addGroup(long id, String text, boolean clickable) {
        this.addGroup(id, text, -1, clickable);
    }

    /**
     * Function to add an empty group
     */
    public void addGroup(long id, String text, int icon, boolean clickable) {

        this.addGroup(id, text, icon, new ArrayList<AnotherExpandableChild>(), clickable);

    }

    /**
     * Function to add a new group
     */
    public void addGroup(long id, String text, int icon, List<AnotherExpandableChild> children, boolean clickable) {

        AnotherExpandableGroup newGroup = new AnotherExpandableGroup();
        newGroup.setId(id);
        newGroup.setText(text);
        newGroup.setIcon(icon);
        newGroup.setChildren(children);
        newGroup.setClickable(clickable);

        groups.add(newGroup);
    }

    /**
     * Function to add a child without icon into a group
     */
    public void addChild(long id, long id_group, String text, boolean clickable) {

        this.addChild(id, id_group, text, -1, clickable);
    }

    /**
     * Function to add a child into a group
     */
    public void addChild(long id, long id_group, String text, int icon, boolean clickable) {

        // Get the parent
        AnotherExpandableGroup parent = getGroupById(id_group);

        AnotherExpandableChild newChild = new AnotherExpandableChild();
        newChild.setId(id);
        newChild.setParent(parent);
        newChild.setText(text);
        newChild.setIcon(icon);
        newChild.setClickable(clickable);

        // Add the child to the parent
        parent.getChildren().add(newChild);

    }

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

    @Override
    public int getChildrenCount(int i) {

        if (groups.size() == 0) {
            return 0;
        } else {

            return groups.get(i).getChildren().size();

        }
    }

    @Override
    public AnotherExpandableGroup getGroup(int i) {

        if (groups.size() > 0) {
            return groups.get(i);
        }

        return null;
    }

    public AnotherExpandableGroup getGroupById(long id) {

        for (AnotherExpandableGroup group : groups) {

            if (group.getId() == id) {
                return group;
            }

        }

        return null;

    }

    @Override
    public AnotherExpandableChild getChild(int group, int item) {

        if (groups.size() > 0) {
            return groups.get(group).getChildren().get(item);
        }

        return null;
    }

    @Override
    public long getGroupId(int i) {

        if (groups.size() > 0) {
            return groups.get(i).getId();
        }

        return 0;
    }

    @Override
    public long getChildId(int group, int item) {

        if (groups.size() > 0) {
            return groups.get(group).getChildren().get(item).getId();
        }

        return 0;
    }

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

    /**
     * Private class to recycle the group views
     */
    private class GroupHolder {
        public ImageView icon;
        public ImageView arrow;
        public TextView text;
    }

    @Override
    public View getGroupView(int group_position, boolean isExpanded, View view, ViewGroup viewGroup) {

        View vi = view;
        AnotherExpandableGroup group = getGroup(group_position);
        GroupHolder holder;

        // Check if the view is recycled
        if (vi == null) {

            // Inflate the view
            vi = LayoutInflater.from(context).inflate(R.layout.parent, null);
            holder = new GroupHolder();

            // Get the references
            holder.icon = (ImageView) vi.findViewById(R.id.icon);
            holder.arrow = (ImageView) vi.findViewById(R.id.arrow);
            holder.text = (TextView) vi.findViewById(R.id.text);

            // Set the tag
            vi.setTag(holder);

        } else {

            // View recycled!!
            holder = (GroupHolder) vi.getTag();
            // Clear the animation
            holder.arrow.clearAnimation();
        }

        // Set the icon
        if (group.getIcon() == -1) {
            // Hide the icon
            holder.icon.setVisibility(View.GONE);
        } else {
            holder.icon.setBackgroundResource(group.getIcon());
        }

        if(!group.isClickable()){
            vi.setClickable(false);
        }

        // Set the arrow style
        if (group.getChildren().isEmpty()) {

            // If we don't have any child, hide the indicator
            holder.arrow.setVisibility(View.GONE);

        } else if (!animating) {

            // Check if the view is animated

            holder.arrow.setVisibility(View.VISIBLE);

            if (group.isAnimate()) {

                if (isExpanded) {
                    // Close the row
                    holder.arrow.startAnimation(openAnimation);
                } else {
                    // Open it
                    holder.arrow.startAnimation(closeAnimation);
                }

                group.setAnimate(false);

            } else {
                holder.arrow.setBackgroundResource(isExpanded ? resOpenArrow : resCloseArrow);
            }

        }

        holder.text.setText(group.getText());

        return vi;

    }

    /**
     * Animation Listener
     */
    @Override
    public void onAnimationStart(Animation animation) {
        this.animating = true;
    }

    @Override
    public void onAnimationEnd(Animation animation) {
        this.animating = false;
        // Invalidate the data to get the correct image
        this.notifyDataSetInvalidated();
    }

    @Override
    public void onAnimationRepeat(Animation animation) {

    }

    /**
     * End animation
     */

    @Override
    public void onGroupExpanded(int position) {

        // Set the animation
        if (getGroup(position) != null) {
            getGroup(position).setAnimate(true);
        }
    }

    @Override
    public void onGroupCollapsed(int position) {
        // Set the animation
        if (getGroup(position) != null) {
            getGroup(position).setAnimate(true);
        }
    }

    /**
     * Private class to recycle the child views
     */
    private class ChildHolder {
        public ImageView icon;
        public TextView text;
    }

    @Override
    public View getChildView(int group_position, int item_position, boolean isExpanded, View view, ViewGroup viewGroup) {

        View vi = view;
        AnotherExpandableChild child = getChild(group_position, item_position);
        ChildHolder holder;

        // Check if the view is recycled
        if (vi == null) {

            // Inflate the view
            vi = LayoutInflater.from(context).inflate(R.layout.child, null);
            holder = new ChildHolder();

            // Get the references
            holder.icon = (ImageView) vi.findViewById(R.id.icon);
            holder.text = (TextView) vi.findViewById(R.id.text);

            // Set the tag
            vi.setTag(holder);

        } else {

            // View recycled!!
            holder = (ChildHolder) vi.getTag();
        }

        // Set the icon
        if (child.getIcon() == -1) {
            // Hide the icon
            holder.icon.setVisibility(View.GONE);
        } else {
            holder.icon.setBackgroundResource(child.getIcon());
        }

        if(!child.isClickable()){
            vi.setClickable(false);
        }


        holder.text.setText(child.getText());

        return vi;

    }

    @Override
    public boolean isChildSelectable(int group, int item) {

        if (groups.size() > 0) {
            return groups.get(group).getChildren().get(item).isClickable();
        }

        return false;
    }
}




Java Source Code List

es.laux.AnotherExpandableAdapter.java
es.laux.AnotherExpandableListView.java
es.laux.Resources.java
es.laux.models.AnotherExpandableChild.java
es.laux.models.AnotherExpandableGroup.java