Skip to main content

Expandable listview in android


Expandable listview in android

public class MainActivity extends Activity {
   
    private SettingsListAdapter adapter;
    private ExpandableListView categoriesList;
    private ArrayList<Category> categories;
   
    protected Context mContext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mContext = this;
        categoriesList = (ExpandableListView)findViewById(R.id.categories);
        categories = Category.getCategories();
        adapter = new SettingsListAdapter(this,
                categories, categoriesList);
        categoriesList.setAdapter(adapter);
       
        categoriesList.setOnChildClickListener(new OnChildClickListener() {
           
            @Override
            public boolean onChildClick(ExpandableListView parent, View v,
                    int groupPosition, int childPosition, long id) {

               
                CheckedTextView checkbox = (CheckedTextView)v.findViewById(R.id.list_item_text_child);
                checkbox.toggle();
               
               
                // find parent view by tag
                View parentView = categoriesList.findViewWithTag(categories.get(groupPosition).name);
                if(parentView != null) {
                    TextView sub = (TextView)parentView.findViewById(R.id.list_item_text_subscriptions);
                   
                    if(sub != null) {
                        Category category = categories.get(groupPosition);
                        if(checkbox.isChecked()) {
                            // add child category to parent's selection list
                            category.selection.add(checkbox.getText().toString());
                           
                            // sort list in alphabetical order
                            Collections.sort(category.selection, new CustomComparator());
                        }
                        else {
                            // remove child category from parent's selection list
                            category.selection.remove(checkbox.getText().toString());
                        }       
                       
                        // display selection list
                        sub.setText(category.selection.toString());
                    }
                }               
                return true;
            }
        });
    }
   
    public class CustomComparator implements Comparator<String> {
        @Override
        public int compare(String o1, String o2) {
            return o1.compareTo(o2);
        }
    }
   
   

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}
---------------------------------------------------------------------------------------------------------------------

public class SettingsListAdapter extends BaseExpandableListAdapter {


    private LayoutInflater inflater;
    private ArrayList<Category> mParent;
    private ExpandableListView accordion;
    public int lastExpandedGroupPosition;   
   

    public SettingsListAdapter(Context context, ArrayList<Category> parent, ExpandableListView accordion) {
        mParent = parent;       
        inflater = LayoutInflater.from(context);
        this.accordion = accordion;      
       
    }


    @Override
    //counts the number of group/parent items so the list knows how many times calls getGroupView() method
    public int getGroupCount() {
        return mParent.size();
    }

    @Override
    //counts the number of children items so the list knows how many times calls getChildView() method
    public int getChildrenCount(int i) {
        return mParent.get(i).children.size();
    }

    @Override
    //gets the title of each parent/group
    public Object getGroup(int i) {
        return mParent.get(i).name;
    }

    @Override
    //gets the name of each item
    public Object getChild(int i, int i1) {
        return mParent.get(i).children.get(i1);
    }

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

    @Override
    public long getChildId(int i, int i1) {
        return i1;
    }

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

    @Override
    //in this method you must set the text to see the parent/group on the list
    public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {
       
        if (view == null) {
            view = inflater.inflate(R.layout.settings_list_item_parent, viewGroup,false);
        }
        // set category name as tag so view can be found view later
        view.setTag(getGroup(i).toString());
       
        TextView textView = (TextView) view.findViewById(R.id.list_item_text_view);
       
        //"i" is the position of the parent/group in the list
        textView.setText(getGroup(i).toString());
       
        TextView sub = (TextView) view.findViewById(R.id.list_item_text_subscriptions);       
       
        if(mParent.get(i).selection.size()>0) {
            sub.setText(mParent.get(i).selection.toString());
        }
        else {
            sub.setText("");
        }
       
        //return the entire view
        return view;
    }
   

    @Override
    //in this method you must set the text to see the children on the list
    public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {
        if (view == null) {
            view = inflater.inflate(R.layout.settings_list_item_child, viewGroup,false);
        }

       
        CheckedTextView textView = (CheckedTextView) view.findViewById(R.id.list_item_text_child);
       
        //"i" is the position of the parent/group in the list and
        //"i1" is the position of the child
        textView.setText(mParent.get(i).children.get(i1).name);       

        // set checked if parent category selection contains child category
        if(mParent.get(i).selection.contains(textView.getText().toString())) {
            textView.setChecked(true);
        }
        else {
            textView.setChecked(false);
        }
       
        //return the entire view
        return view;
    }

    @Override
    public boolean isChildSelectable(int i, int i1) {
        return true;
    }
   
    @Override
    /**
     * automatically collapse last expanded group
     * @see http://stackoverflow.com/questions/4314777/programmatically-collapse-a-group-in-expandablelistview
     */   
    public void onGroupExpanded(int groupPosition) {
       
        if(groupPosition != lastExpandedGroupPosition){
            accordion.collapseGroup(lastExpandedGroupPosition);
        }
       
        super.onGroupExpanded(groupPosition);
    
        lastExpandedGroupPosition = groupPosition;
       
    }
   
   
   
}

 

Comments

Popular posts from this blog

Custom camera using SurfaceView android with autofocus & auto lights & more

Custom camera using SurfaceView android with autofocus & auto lights & much more /**  * @author Tatyabhau Chavan  *  */ public class Preview extends SurfaceView implements SurfaceHolder.Callback {     private SurfaceHolder mHolder;     private Camera mCamera;     public Camera.Parameters mParameters;     private byte[] mBuffer;     private Activity mActivity;     // this constructor used when requested as an XML resource     public Preview(Context context, AttributeSet attrs) {         super(context, attrs);         init();     }     public Preview(Context context) {         super(context);         init();     }     public Camera getCamera() {        ...

Bitmap scalling and cropping from center

How to Bitmap scalling and cropping from center? public class ScalingUtilities {     /**      * Utility function for decoding an image resource. The decoded bitmap will      * be optimized for further scaling to the requested destination dimensions      * and scaling logic.      *      * @param res      *            The resources object containing the image data      * @param resId      *            The resource id of the image data      * @param dstWidth      *            Width of destination area      * @param dstHeight      *     ...

Improving Layout Performance

Improving Layout Performance Layouts are a key part of Android applications that directly affect the user experience. If implemented poorly, your layout can lead to a memory hungry application with slow UIs. The Android SDK includes tools to help you identify problems in your layout performance, which when combined the lessons here, you will be able to implement smooth scrolling interfaces with a minimum memory footprint. Lessons Optimizing Layout Hierarchies In the same way a complex web page can slow down load time, your layout hierarchy if too complex can also cause performance problems. This lesson shows how you can use SDK tools to inspect your layout and discover performance bottlenecks. Re-using Layouts with <include/> If your application UI repeats certain layout constructs in multiple places, this lesson shows you how to create efficient, re-usable layout constructs, then include them in the appropriate UI layouts. Loading Views On Demand Be...