When the android mobile phone rotates the screen, the number of columns in the GridView and the column width are adaptive. androidgridview
I accidentally opened the code of an android application that I had done a year ago. I can see a small Function Point (such as a question) in it. I am writing an article to take a note. At that time, the problem was that when rotating the screen, the number of columns and width of the gridview should be adaptive to the screen width, and the spacing between each cell should be kept. Because the screen width of each mobile phone is different, after the cell width and spacing are specified, the number of cells in each row cannot be determined, the number must be calculated at run time. Similarly, the cell width and spacing we set cannot be exactly within the screen width. To solve this problem, a simple algorithm is designed, first, you need to specify the cell width and spacing in advance, and then calculate the adjusted cell width based on the screen width and the number of cells per row. For each cell, the error between the Adjusted Width and the initial width is less than: initial cell width/(2 * Number of cells per row after adjustment). When the cell width is less than the screen width, the maximum error is: initial cell width/4. After experiments, the adaptive problem is well solved.
The algorithm flow is as follows:
In this way, we implement a small algorithm by specifying the column spacing (which does not need to be set to zero) and the initial column width (this value is just an approximate width ), during the operation, you can automatically calculate the exact column width and number of columns that can be accommodated on the Screen Based on the screen width of the mobile phone, thus shielding the screen sizes of many mobile phones and achieving adaptive purposes, this method can also be applied to other similar scenarios.
Is to draw a sketch when analyzing the problem:
① It is half of the initial cell width that exceeds the screen width and corresponds to steps 3rd, 4, and 6 of the algorithm flow.
② If the screen width is greater than half the initial width of the cell, it corresponds to steps 3rd, 5, and 6 of the algorithm flow.
Public class FragmentAllBushou extends Fragment {GridView gridView = null; @ Override public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {WindowManager manger = getActivity (). getWindowManager (); Display display = manager. getdefadisplay display (); // screen height int screenHeight = display. getHeight (); // screen width int screenWidth = display. getWidth (); gridView = new GridV Iew (getActivity (); ColumnInfo colInfo = calculateColumnWidthAndCountInRow (screenWidth, 100,2); int rowNum = cursor. getCount () % colInfo. countInRow = 0? Cursor. getCount ()/colInfo. countInRow: cursor. getCount ()/colInfo. countInRow + 1; gridView. setLayoutParams (new LayoutParams (screenWidth, rowNum * colInfo. width + (rowNum-1) * 2); gridView. setNumColumns (colInfo. countInRow); gridView. setGravity (Gravity. CENTER); gridView. setHorizontalSpacing (2); gridView. setVerticalSpacing (2); gridView. setStretchMode (GridView. STRETCH_COLUMN_WIDTH);} // stores the calculated cell Information class ColumnInfo {// cell width public int width = 0; // The number of cells per row. public int countInRow = 0;}/*** according to the cell phone screen width, calculate the width of each cell in the gridview * @ param screenWidth screen width * @ param width cell preset width * @ param padding cell spacing * @ return */private ColumnInfo calculateColumnWidthAndCountInRow (int screenWidth, int width, int padding) {ColumnInfo colInfo = new ColumnInfo (); int colCount = 0; // determines whether the screen can accommodate the next integer cell. If not, save the Extra width to int space = screenWidth % width; if (space = 0) {// exactly fit the lower colCount = screenWidth/width ;} else if (space> = (width/2) {// when the Extra width is greater than half of the cell width, the last cell is removed, divide the width it occupies and add it to each other cell. colCount = screenWidth/width; space = width-space; width = width + space/colCount ;} else {// when the Extra width is less than half of the cell width, the extra width is evenly divided, and each cell minus the width colCount = screenWidth/width + 1; width = width-space/colCount;} colInfo. countInRow = colCount; // calculates the total spacing width of each row, and adjusts the colInfo width based on the number of cells. width = width-(colCount + 1) * padding)/colCount; return colInfo ;}}