How to install Android GridView with different column sizes

I have a gridview in my main layout as follows:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <GridView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/gridview" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:gravity="center" android:horizontalSpacing="1dp" android:stretchMode="columnWidth" android:verticalSpacing="1dp" /> </LinearLayout> 

which creates a grid like this enter image description here

the contents of each cell are as follows:

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="match_parent" android:background="@drawable/btn_measurement" > <Button android:id="@+id/numerical" android:layout_width="match_parent" android:layout_height="match_parent" android:text="---" /> </LinearLayout> 

But I want the fourth column to make up one third of the other columns, and I just can't.

I checked the links:

GridView with different column sizes and Android GridView Column Stretching

but they didn’t help. :(

if you think that I need to completely change my mind, please clarify how to just give a shared key. Thanx

+4
source share
3 answers

use this code to reduce the size of the column, but here you need to apply some logic. in the 4th column you want it to be small, so you need to override the getView () method and then use some variable and check if this variable is 3 or 7 or 11 ... then make the button size small. I think I have my point ....

  int logic =3; public View getView(int position, View convertView, ViewGroup parent) { Button btn; LinearLayout layout; if (convertView == null) { convertView = inflate the layout and initialize convertview if(position % logic==0) converView..setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT, (float) 0.3)); btn = new Button(context); btn.setGravity(Gravity.RIGHT); btn.setPadding(5, 5, 5, 5); layout = new LinearLayout(context); layout.setOrientation(0); layout.setPadding(5, 5, 5, 10); btn.setText(names[position]); layout.addView(btn); } else { layout = (LinearLayout) convertView; btn = (Button) layout.getChildAt(0); btn.setText(names[position]); } return layout; } 
+1
source

Use a RecyclerView (instead of a GridView) with a custom GridLayoutManager, as shown here in the Variable Range Size section: http://blog.sqisland.com/2014/12/recyclerview-grid-with-header.html

Basically you make each regular column 3x in size and the smaller column takes 1x.

 GridLayoutManager manager = new GridLayoutManager(this, 10); // 3 cols of 3 + 1 col of 1 manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() { @Override public int getSpanSize(int position) { if (position % 4 == 3) return 1; // Fourth column is 1x else return 3; // Remaining columns are 3x } }); recyclerView.setLayoutManager(manager); 
+1
source

I am using this solution

GridView with different column sizes

Using the List view instead of the Grid view, and place 4 buttons in one horizontal line layout for each item. But you need more work to detect button clicks, but I think it is doable

Another work I can find is to use a third-party library like this one https://github.com/felipecsl/AsymmetricGridView

0
source

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


All Articles