GridView with different column sizes

Trying to make a highscore list that scrolls and looks like this:

foo 1000 bar 876 foobar 500 foobarfoo 1 

I am currently doing this using a GridView. I would like to set the column width of the name to 60% of the screen and the column width of the account to 40%. Is it possible?

I'm currently trying to use the costum adapter. Here is the getview function for it:

 public View getView(int position, View convertView, ViewGroup parent) { TextView tv; if (convertView == null) { tv = new TextView(context); tv.setTextSize(25); tv.setTextColor(Color.WHITE); if (position % 2 == 0) { tv.setLayoutParams(new GridView.LayoutParams((width/10)*6, 50)); } else { tv.setLayoutParams(new GridView.LayoutParams((width/10)*4, 50)); } } else { tv = (TextView) convertView; } tv.setText(texts[position]); return tv; } 

The layout is built on the grid and the button below. XML file:

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:id="@+id/top"> <GridView android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="2" android:id="@+id/grid" android:numColumns="2" android:columnWidth="0dp" > </GridView> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/backbutton" android:text="@string/backstr"></Button> </LinearLayout> 

So the question is again: is it possible to install a GridView to add columns of different sizes? If so, is my approach good? (Probably not, since it does not work :)) Did I miss something?

Thank you in advance!

+7
source share
2 answers

I work like a charm! Many thanks to Abhinav, who advised well. Here is the code for anyone with the same problem:

 public View getView(int position, View convertView, ViewGroup parent) { TextView tv1; TextView tv2; LinearLayout ll; if (convertView == null) { tv1 = new TextView(context); tv1.setTextSize(25); tv1.setTextColor(Color.WHITE); tv1.setGravity(Gravity.LEFT); tv1.setPadding(5, 5, 5, 5); tv1.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT, (float) 3.0)); tv2 = new TextView(context); tv2.setTextSize(25); tv2.setTextColor(Color.WHITE); tv2.setGravity(Gravity.RIGHT); tv2.setPadding(5, 5, 5, 5); tv2.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT, (float) 4.0)); ll = new LinearLayout(context); ll.setOrientation(0); ll.setPadding(5, 5, 5, 10); tv1.setText(names[position]); tv2.setText(scores[position]); ll.addView(tv1); ll.addView(tv2); } else { ll = (LinearLayout) convertView; tv1 = (TextView) ll.getChildAt(0); tv2 = (TextView) ll.getChildAt(1); tv1.setText(names[position]); tv2.setText(scores[position]); } return ll; } 

And the XML file:

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:id="@+id/top"> <ListView android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="2" android:numColumns="2" android:columnWidth="0dp" android:id="@+id/list"> </ListView> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/backbutton" android:text="@string/back"></Button> </LinearLayout> 

If you think you can improve this code (since I'm new to this area), feel free to answer! Thanks in advance!

+1
source

We had to do this for the project and did not want to use anything other than the GridView , because the functionality was repeated, but on one GridView we needed a slightly different view configuration (but the adapter and all that other good things were still used). We found that you can do this if you override the GridView layoutChildren function, although it is rather poorly documented.

In our implementation, there is a check of the special presentation and if the new layout has already been implemented:

 @Override protected void layoutChildren(){ super.layoutChildren(); if(!isSpecial || layoutAlreadySet) return; layoutAlreadySet = true; //Implement special layout.... } 
+5
source

All Articles