Reducing the number of gridlayout rows / columns in android

I need a dynamic gridlayout that can switch between 3 to 3 and 4 to 4. I can set RowCount and setColumnCount from 3 to 4, but not from 4 to 3. The following problem will be displayed on it:

Called: java.lang.IllegalArgumentException: rowCount must be greater than or equal to the maximum number of all grid indices (and intervals) defined in the LayoutParams of each child.

Is there any work to achieve this using gridlayout.

Thanks at Advance.

+6
source share
3 answers

I understand that this question is quite old, but for people who still encounter this exception today, I will provide an explanation that may shed light on how GridLayout is getting shorter and why I think it / throws an exception for OP .

In short:

GridLayout views can, after reduction, occupy cells that are not part of the GridLayout grid, which raises the IllegalArgumentException mentioned by OP. To avoid this, remove the child views that will occupy cells outside the GridLayout grid until the actual call to setRowCount() or setColumnCount() . This can be done using GridLayout.removeView(aboutToBeIllegalChild); or by clearing the entire layout using GridLayout.removeAllViews(); .

In the long:

Everything that calls the GridLayout.setRowCount() call sets the new number of rows that the layout should contain. However, it does not interfere with the child views that GridLayout currently contains, and does not specify Spec (which columns and rows the child view occupies).

Which exception basically tells us and the documents confirm, is that GridLayout does not allow any of its child views to occupy cells that are outside the GridLayout grid. As an example, the layout will not allow the child view to occupy the cell (5, 1) when the grid is only 4 x 1 .

This leads us to why the original poster was successful at dynamically increasing the size of the GridLayout , but unsuccessfully at decreasing it. If you increase the size, any child views that were already bound to the GridLayout with the specified cells will still be placed in the left cells if the grid receives additional rows or columns dynamically. When mesh sizes are reduced, children's views that were placed in cells that would disappear as a result of deleting rows or columns will now be considered illegal.

To get around this, you must either first remove those (child) child views from your parent GridLayout by calling GridLayout.removeView(aboutToBeIllegalChild); , or simply erase all GridLayout by calling GridLayout.removeAllViews(); .

Hope this helps!

+6
source

Based on Teun Kooijman's answer, you can simply change the Spectrum in GridLayout.LayoutParams and save all Views inside the GridLayout :

 private void changeColumnCount(int columnCount) { if (gridLayout.getColumnCount() != columnCount) { final int viewsCount = gridLayout.getChildCount(); for (int i = 0; i < viewsCount; i++) { View view = gridLayout.getChildAt(i); //new GridLayout.LayoutParams created with Spec.UNSPECIFIED //which are package visible view.setLayoutParams(new GridLayout.LayoutParams()); } gridLayout.setColumnCount(columnCount); } } 

You can also change Spec in another way by contacting GridLayout.LayoutParams.rowSpec and GridLayout.LayoutParams.columnSpec

+1
source

For me, the problem was changing the number of GridView columns when the application changes orientation. I achieved this by putting the following code in public void onConfigurationChanged(Configuration newConfig) .

 if (mGridLayout.getColumnCount() != getResources().getInteger(R.integer.nav_columns)) { final int viewsCount = mGridLayout.getChildCount(); for (int i = 0; i < viewsCount; i++) { View view = mGridLayout.getChildAt(i); GridLayout.LayoutParams layoutParams = new GridLayout.LayoutParams(); int colIndex = i%getResources().getInteger(R.integer.nav_columns); int rowIndex = i/getResources().getInteger(R.integer.nav_columns); layoutParams.height = LinearLayout.LayoutParams.WRAP_CONTENT; layoutParams.width = 0; layoutParams.columnSpec = GridLayout.spec(colIndex,1,GridLayout.FILL,1f); layoutParams.rowSpec = GridLayout.spec(rowIndex); view.setLayoutParams(layoutParams); } mGridLayout.setColumnCount(getResources().getInteger(R.integer.nav_columns)); } 

Layout parameter values ​​may need to be changed depending on your needs.

+1
source

Source: https://habr.com/ru/post/977099/


All Articles