SHARE
TWEET

GridView with different column sizes

a guest Mar 30th, 2012 2,005 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. foo         1000
  2. bar         876
  3. foobar      500
  4. foobarfoo   1
  5.        
  6. public View getView(int position, View convertView, ViewGroup parent) {
  7.     TextView tv;
  8.     if (convertView == null) {
  9.         tv = new TextView(context);
  10.         tv.setTextSize(25);
  11.         tv.setTextColor(Color.WHITE);              
  12.  
  13.         if (position % 2 == 0)
  14.         {
  15.             tv.setLayoutParams(new GridView.LayoutParams((width/10)*6, 50));
  16.         }
  17.         else
  18.         {
  19.             tv.setLayoutParams(new GridView.LayoutParams((width/10)*4, 50));
  20.         }
  21.     }
  22.     else {
  23.         tv = (TextView) convertView;
  24.     }
  25.  
  26.     tv.setText(texts[position]);
  27.     return tv;
  28. }
  29.        
  30. <?xml version="1.0" encoding="utf-8"?>
  31. <LinearLayout
  32.   xmlns:android="http://schemas.android.com/apk/res/android"
  33.   android:layout_width="fill_parent"
  34.   android:layout_height="fill_parent" android:orientation="vertical" android:id="@+id/top">
  35.     <GridView android:layout_width="fill_parent"
  36.               android:layout_height="wrap_content"
  37.               android:layout_weight="2"
  38.               android:id="@+id/grid"
  39.               android:numColumns="2"
  40.               android:columnWidth="0dp"
  41.               >
  42.     </GridView>
  43.     <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/backbutton" android:text="@string/backstr"></Button>
  44.  
  45. </LinearLayout>
  46.        
  47. public View getView(int position, View convertView, ViewGroup parent) {
  48.     TextView tv1;
  49.     TextView tv2;
  50.     LinearLayout ll;
  51.     if (convertView == null) {
  52.         tv1 = new TextView(context);
  53.         tv1.setTextSize(25);
  54.         tv1.setTextColor(Color.WHITE);
  55.         tv1.setGravity(Gravity.LEFT);
  56.         tv1.setPadding(5, 5, 5, 5);
  57.         tv1.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,
  58.                                                           LinearLayout.LayoutParams.FILL_PARENT,
  59.                                                           (float) 3.0));
  60.  
  61.         tv2 = new TextView(context);
  62.         tv2.setTextSize(25);
  63.         tv2.setTextColor(Color.WHITE);  
  64.         tv2.setGravity(Gravity.RIGHT);
  65.         tv2.setPadding(5, 5, 5, 5);
  66.         tv2.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,
  67.                                                               LinearLayout.LayoutParams.FILL_PARENT,
  68.                                                               (float) 4.0));
  69.  
  70.         ll = new LinearLayout(context);
  71.         ll.setOrientation(0);
  72.         ll.setPadding(5, 5, 5, 10);
  73.  
  74.         tv1.setText(names[position]);
  75.         tv2.setText(scores[position]);
  76.  
  77.         ll.addView(tv1);
  78.         ll.addView(tv2);
  79.     }
  80.     else {
  81.         ll = (LinearLayout) convertView;
  82.         tv1 = (TextView) ll.getChildAt(0);
  83.         tv2 = (TextView) ll.getChildAt(1);
  84.  
  85.         tv1.setText(names[position]);
  86.         tv2.setText(scores[position]);
  87.     }
  88.  
  89.     return ll;
  90. }
  91.        
  92. <?xml version="1.0" encoding="utf-8"?>
  93. <LinearLayout
  94.   xmlns:android="http://schemas.android.com/apk/res/android"
  95.   android:layout_width="fill_parent"
  96.   android:layout_height="fill_parent" android:orientation="vertical" android:id="@+id/top">
  97.     <ListView android:layout_width="fill_parent"
  98.               android:layout_height="wrap_content"
  99.               android:layout_weight="2"
  100.               android:numColumns="2"
  101.               android:columnWidth="0dp"
  102.               android:id="@+id/list">
  103.     </ListView>
  104.     <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/backbutton" android:text="@string/back"></Button>
  105.  
  106. </LinearLayout>
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top