GridView inside Expandable list in android

守給你的承諾、 提交于 2019-11-28 03:20:25

I've done tons of searches my own for solving this situation, but didn't find anything so far, so i've made a workaround.

This workaround assumes that you know / can retrieve the width of your columns, and the height of your grid cell renderers (the dp size set in your layout xmls).

You should insert a few more lines inside your ExpandableListAdapter's getChildView method:

@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent)
{
    ViewGroup item = getViewGroupChild(convertView, parent);
    GridView label = (GridView) item.findViewById(ipvc.estg.placebook.R.id.gridview);
    label.setAdapter(new GridAdapter(parent.getContext(), groupPosition+1));

    // initialize the following variables (i've done it based on your layout
    // note: rowHeightDp is based on my grid_cell.xml, that is the height i've
    //    assigned to the items in the grid.
    final int spacingDp = 10;
    final int colWidthDp = 50;
    final int rowHeightDp = 20;

    // convert the dp values to pixels
    final float COL_WIDTH = getBaseContext().getResources().getDisplayMetrics().density * colWidthDp;
    final float ROW_HEIGHT = getBaseContext().getResources().getDisplayMetrics().density * rowHeightDp;
    final float SPACING = getBaseContext().getResources().getDisplayMetrics().density * spacingDp;

    // calculate the column and row counts based on your display
    final int colCount = (int)Math.floor((parentView.getWidth() - (2 * SPACING)) / (COL_WIDTH + SPACING));
    final int rowCount = (int)Math.ceil((intValues.size() + 0d) / colCount);

    // calculate the height for the current grid
    final int GRID_HEIGHT = Math.round(rowCount * (ROW_HEIGHT + SPACING));

    // set the height of the current grid
    label.getLayoutParams().height = GRID_HEIGHT;

    return item;
}

With the addition above i was able to produce the following displayed layouts:

and

I hope it can help you as well.

shimi uhlman

all you need to do is change the GrideView with ExpandableHeightGridView

and configure XML with

android:isScrollContainer="false"

and in code

expandableHeightGridView.setExpanded(true);

how about using a Gallery instead of GridView? check this out!

Try this github full source code of expendable list view with gridview

https://github.com/coolwd10/ExpandablelistwithGirdview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!