Example usage for android.widget GridView getNumColumns

List of usage examples for android.widget GridView getNumColumns

Introduction

In this page you can find the example usage for android.widget GridView getNumColumns.

Prototype

@ViewDebug.ExportedProperty
public int getNumColumns() 

Source Link

Document

Get the number of columns in the grid.

Usage

From source file:Main.java

public static void setGridViewSize(GridView grid) {
    ListAdapter myListAdapter = grid.getAdapter();
    if (myListAdapter == null) {
        //do nothing return null
        return;//www . j  ava  2  s . c  om
    }

    //set listAdapter in loop for getting final size
    int desiredWidth = View.MeasureSpec.makeMeasureSpec(grid.getWidth() / grid.getNumColumns(),
            View.MeasureSpec.AT_MOST);
    int totalHeight = 0;
    View view = null;
    for (int i = 0; i < ((myListAdapter.getCount() / grid.getNumColumns())
            + myListAdapter.getCount() % grid.getNumColumns()); i++) {
        view = myListAdapter.getView(i, view, grid);
        if (i == 0) {
            view.setLayoutParams(new GridView.LayoutParams(desiredWidth, ViewGroup.LayoutParams.WRAP_CONTENT));
        }
        view.measure(desiredWidth, View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
        totalHeight += view.getMeasuredHeight();
    }

    //setting listview item in adapter
    ViewGroup.LayoutParams params = grid.getLayoutParams();
    params.height = totalHeight
            + (grid.getHorizontalSpacing() * (((myListAdapter.getCount() - 1) / grid.getNumColumns())
                    + (myListAdapter.getCount() % grid.getNumColumns())));
    grid.setLayoutParams(params);
}

From source file:util.Utils.java

/**
 * gridview// w w w .j a  va2s. com
 *
 * @param listView
 */
public static void setListViewHeightBasedOnChildren(GridView listView) {
    // ?listviewadapter
    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null) {
        return;
    }
    // 
    int col = listView.getNumColumns();
    int totalHeight = 0;
    // i?4listAdapter.getCount()?4 item
    // listAdapter.getCount()?8
    for (int i = 0; i < listAdapter.getCount(); i += col) {
        // ?listview?item
        View listItem = listAdapter.getView(i, null, listView);
        listItem.measure(0, 0);
        // ?item
        totalHeight += listItem.getMeasuredHeight();
    }
    totalHeight += listView.getVerticalSpacing();

    // ?listview?
    ViewGroup.LayoutParams params = listView.getLayoutParams();
    // 
    params.height = totalHeight;
    // margin
    //        ((ViewGroup.MarginLayoutParams) params).setMargins(10, 10, 10, 10);
    // ?
    listView.setLayoutParams(params);
}

From source file:com.github.jvanhie.discogsscrobbler.ReleaseListFragment.java

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    //call listener to determine correct width when available or changed
    getView().getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override/*from   w  w  w  .j  a v a 2  s .  c  om*/
        public void onGlobalLayout() {
            int fragmentWidth = getView().getWidth();
            if (fragmentWidth > 0) {
                if (mGrid) {
                    GridView grid = (GridView) mList;
                    int col = grid.getNumColumns();
                    int newCol = Math.round((fragmentWidth / logicalDensity) / (float) 150);
                    if (col != newCol) {
                        int pos = grid.getFirstVisiblePosition();
                        //if an item is selected, focus on this instead
                        if (mActivatedPosition != ListView.INVALID_POSITION)
                            pos = mActivatedPosition;
                        grid.setNumColumns(newCol);
                        grid.setSelection(pos);
                    }
                }
            }
        }
    });
}

From source file:cx.ring.fragments.CallListFragment.java

public void setGridViewHeight(GridView gridView, LinearLayout llMain) {
    ListAdapter listAdapter = gridView.getAdapter();
    if (listAdapter == null) {
        return;/*from  w w  w .  ja v a2s.c  o  m*/
    }

    int totalHeight = 0;
    int firstHeight = 0;
    int desiredWidth = View.MeasureSpec.makeMeasureSpec(gridView.getWidth(), View.MeasureSpec.AT_MOST);

    int rows = (listAdapter.getCount() + gridView.getNumColumns() - 1) / gridView.getNumColumns();

    for (int i = 0; i < rows; i++) {
        if (i == 0) {
            View listItem = listAdapter.getView(i, null, gridView);
            listItem.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
            firstHeight = listItem.getMeasuredHeight();
        }
        totalHeight += firstHeight;
    }

    LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) llMain.getLayoutParams();

    params.height = (int) (totalHeight
            + (getResources().getDimension(R.dimen.contact_vertical_spacing) * (rows - 1)
                    + llMain.getPaddingBottom() + llMain.getPaddingTop()));
    llMain.setLayoutParams(params);
    mHeader.requestLayout();
}