Implementation of Android Expandablegridview

Source: Internet
Author: User

Recently, when I was doing a project, I ran into a layout

In Android there is a way to implement the folding list is expandablelistview but the official no Expandablegridview

So how do you use Expandablelistview to achieve a expandablegridview?

The approximate principle is: to display a row in each Expandablelistview, use this line to load a GridView

accreditations, put the code on.

1. Load a Expandablelistview

Private Simpledataadapter Simpledataadapter; Private Expandablelistview Mexpandablelistview;        Mexpandablelistview = (Expandablelistview) Findviewbyid (r.id.expandablelist);        Msuperadapter = new Supervesedataexpandableadapter (this,msupervesedata);        Mexpandablelistview.setadapter (msuperadapter);        Mexpandablelistview.expandgroup (0);  

2. Rewrite Baseexpandablelistadapter

overriding Baseexpandablelistadapter, mostly overriding the Getchildview method to load a GridView dynamic calculation of the GridView height

    @Override public View getchildview (int groupposition, int childposition, Boolean islastchild, View Convertview, Vie        Wgroup parent) {if (Convertview = = null) {Convertview = layoutinflater.inflate (R.layout.view, NULL);        } Customgridview GridView = (customgridview) convertview. Findviewbyid (R.id.gridview_toolbar);        Gridview.setnumcolumns (2);        Gridview.sethorizontalspacing (10);        Gridview.setgravity (Gravity.center);        Gridadapter adapter = new Gridadapter (context, Supervesedatas.get (groupposition). Controldatas);       Gridview.setadapter (adapter);//Adapter int totalheight = 0; for (int size = 0; size < Adapter.getcount (); size++) {Relativelayout relativelayout = (relativelayout) Ada            Pter.getview (size, null, GridView);            TextView TextView = (TextView) relativelayout.getchildat (0);            Textview.measure (0, 0);            Totalheight + = Textview.getmeasuredheight (); if (size = = Adapter.getcount ()-1) {if (size%2 = = 0) {totalheight + = Textview.getmeasuredheight () *                2;        }}} gridview.setheight (Totalheight);    return convertview; }

Another place to be aware of is:

Getchildrencount returns 1 because the GridView is loaded with an item
@Override public    int getchildrencount (int groupposition) {        return  1;    }

  

The result of the final implementation is as follows

Implementation of Android Expandablegridview

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.