bug fix record swipe + action bar color changed
authordrizea <danielrizea27@gmail.com>
Mon, 30 Jul 2012 21:24:08 +0000 (00:24 +0300)
committerdrizea <danielrizea27@gmail.com>
Mon, 30 Jul 2012 21:24:08 +0000 (00:24 +0300)
13 files changed:
Open-ILS/src/Android/AndroidManifest.xml
Open-ILS/src/Android/res/drawable/grey_rounded_corners.xml [new file with mode: 0644]
Open-ILS/src/Android/res/drawable/menu_button.xml
Open-ILS/src/Android/res/layout-land/search_result_list.xml [new file with mode: 0644]
Open-ILS/src/Android/res/layout/bookbag_spinner.xml
Open-ILS/src/Android/res/layout/search_result_list.xml
Open-ILS/src/Android/res/values/colors.xml
Open-ILS/src/Android/res/values/styles.xml
Open-ILS/src/Android/src/org/evergreen/android/searchCatalog/SearchCatalogListView.java
Open-ILS/src/Android/src/org/evergreen/android/utils/ui/BaseSampleActivity.java
Open-ILS/src/Android/src/org/evergreen/android/utils/ui/BasicDetailsFragment.java
Open-ILS/src/Android/src/org/evergreen/android/utils/ui/Dashboard.java [new file with mode: 0644]
Open-ILS/src/Android/src/org/evergreen/android/utils/ui/TestFragmentAdapter.java

index b55c33f..720ee4a 100644 (file)
@@ -36,7 +36,8 @@
             ></activity>
         <activity
             android:name=".searchCatalog.SearchCatalogListView"
-            android:label="@string/app_name">
+            android:label="@string/app_name"
+            >
                
         </activity>
         <activity android:name="org.evergreen.android.views.ApplicationPreferences"></activity>
diff --git a/Open-ILS/src/Android/res/drawable/grey_rounded_corners.xml b/Open-ILS/src/Android/res/drawable/grey_rounded_corners.xml
new file mode 100644 (file)
index 0000000..2bbf8d7
--- /dev/null
@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8"?> 
+<shape xmlns:android="http://schemas.android.com/apk/res/android" 
+     android:shape="rectangle"> 
+     <gradient android:startColor="@color/grey" android:endColor="@color/grey" 
+            android:angle="270"/> 
+
+    <corners android:radius="5dip" /> 
+</shape> 
\ No newline at end of file
index fade6a4..1bc2122 100644 (file)
@@ -5,9 +5,9 @@
             <gradient android:startColor="@color/pres1"
                 android:endColor="@color/pres2" android:angle="270" />
             <stroke android:width="5dp" android:color="@color/stro3" />
-               <corners android:radius="10dp" android:topRightRadius="80dp" android:bottomLeftRadius="80dp" />
-            <padding android:left="10dp" android:top="0dp"
-                android:right="10dp" android:bottom="0dp" />
+               <corners android:radius="5dp" android:topRightRadius="5dp" android:bottomLeftRadius="5dp" />
+            <padding android:left="5dp" android:top="0dp"
+                android:right="5dp" android:bottom="0dp" />
         </shape>
     </item>
     <item android:state_focused="true">
             <gradient android:endColor="@color/focu1"
                 android:startColor="@color/focu2" android:angle="270" />
             <stroke android:width="5dp" android:color="@color/stro2" />
-                       <corners android:radius="10dp" android:topRightRadius="80dp" android:bottomLeftRadius="80dp" />
-            <padding android:left="10dp" android:top="0dp"
-                android:right="10dp" android:bottom="0dp" />
+                       <corners android:radius="5dp" android:topRightRadius="5dp" android:bottomLeftRadius="5dp" />
+            <padding android:left="5dp" android:top="0dp"
+                android:right="5dp" android:bottom="0dp" />
         </shape>
     </item>
     <item>
         <shape>
             <gradient android:endColor="@color/norm1"
                 android:startColor="@color/norm2" android:angle="270" />
-            <corners android:radius="10dp" android:topRightRadius="80dp" android:bottomLeftRadius="80dp" />
-            <padding android:left="10dp" android:top="0dp"
-                android:right="10dp" android:bottom="0dp" />
+            <corners android:radius="5dp" android:topRightRadius="5dp" android:bottomLeftRadius="5dp" />
+            <padding android:left="5dp" android:top="0dp"
+                android:right="5dp" android:bottom="0dp" />
         </shape>
     </item>
 
diff --git a/Open-ILS/src/Android/res/layout-land/search_result_list.xml b/Open-ILS/src/Android/res/layout-land/search_result_list.xml
new file mode 100644 (file)
index 0000000..ab0dd08
--- /dev/null
@@ -0,0 +1,146 @@
+<?xml version="1.0" encoding="utf-8"?>
+<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    android:layout_width="fill_parent"
+    android:layout_height="fill_parent">
+
+    <RelativeLayout
+        android:id="@+id/header"
+        android:layout_width="fill_parent"
+        android:layout_height="wrap_content"
+        android:orientation="horizontal"
+        android:background="@drawable/header_rounded_corners"
+        android:paddingTop="3dip"
+        >
+        
+        <ImageButton 
+            android:id="@+id/library_logo"
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            style="@style/LibraryLogoText"
+            android:layout_alignParentLeft="true"
+            android:src="@drawable/evergreen_launcher_icon_48"
+            android:layout_marginLeft="5dip"
+            />
+
+        <Button
+            android:id="@+id/my_acount_button"
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:text="@string/my_account_button_text"
+            android:layout_alignParentRight="true"
+            android:padding="10dip"
+            android:layout_marginTop="5dip"
+            />
+
+
+        <LinearLayout
+            android:orientation="horizontal"
+            android:layout_width="fill_parent"
+            android:layout_height="wrap_content"
+            android:padding="4dp"
+            android:id="@+id/search_text_container"
+            android:focusable="true" 
+               android:focusableInTouchMode="true"
+            android:layout_below="@id/library_logo">
+
+            <EditText
+                    android:id="@+id/searchText"
+                    android:layout_width="0dip"
+                    android:layout_height="wrap_content"
+                    android:hint="@string/search_hint"
+                    android:singleLine="true"
+                    android:layout_weight="1"/>
+
+        </LinearLayout>
+        
+        <LinearLayout 
+            android:id="@+id/search_functions"
+            android:layout_width="fill_parent"
+            android:layout_height="wrap_content"
+            android:layout_below="@id/search_text_container">
+            
+                <Spinner 
+                               android:id="@+id/chose_organisation"   
+                               android:layout_width="fill_parent"
+                               android:layout_height="wrap_content"
+                               android:layout_gravity="center_horizontal"
+                               android:layout_weight="1"/>
+               
+                <ImageButton
+                    android:id="@+id/searchButton"
+                    android:layout_width="wrap_content"
+                    android:layout_height="wrap_content"
+                    android:src="@drawable/action_search"
+
+                    android:paddingLeft="10dp"
+                    android:paddingRight="10dp"
+                    android:paddingTop="5dp"
+                    android:paddingBottom="5dp"/> 
+
+        </LinearLayout>
+
+    </RelativeLayout>
+
+
+     
+     <LinearLayout 
+         android:id="@+id/search_preference_options"
+         android:layout_width="fill_parent"
+         android:layout_height="wrap_content"
+         android:orientation="horizontal"
+         android:layout_below="@id/header"
+         android:layout_centerHorizontal="true"
+         >
+
+             
+             <Button 
+                android:id="@+id/menu_advanced_search_button"
+             android:layout_width="100dip"
+             android:layout_height="wrap_content"
+             android:text="@string/menu_button_advanced_search"
+             android:drawableTop="@drawable/settings_small"
+             style="@style/MenuListButton"
+             />
+         
+            <Button 
+             android:id="@+id/library_hours_button"
+             android:layout_width="100dip"
+             android:layout_height="wrap_content"
+             android:text="@string/menu_button_library_hours"
+             android:drawableTop="@drawable/settings_small"
+             style="@style/MenuListButton"
+             />    
+                 
+            
+                 
+             <Button
+             android:id="@+id/preference_button" 
+             android:layout_width="100dip"
+             android:layout_height="wrap_content"
+             android:text="@string/menu_button_application_preferences"
+             android:drawableTop="@drawable/settings_small"
+             style="@style/MenuListButton"
+             />
+            
+     </LinearLayout>
+          <LinearLayout android:layout_width="fill_parent"
+         android:layout_height="wrap_content"
+         android:id="@+id/search_text"
+         android:layout_below="@id/search_preference_options"
+         >
+         <TextView 
+             android:id="@+id/search_result_number"
+             android:layout_width="wrap_content"
+             android:layout_height="wrap_content"
+             android:textStyle="bold"
+             />
+         
+     </LinearLayout>
+     
+       <ListView 
+         android:id="@+id/search_results_list"
+         android:layout_width="fill_parent"
+         android:layout_height="fill_parent"
+         android:layout_below="@id/search_text"  
+         ></ListView>
+</RelativeLayout>
\ No newline at end of file
index 0e9f974..4096791 100644 (file)
@@ -1,7 +1,7 @@
 <?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="200dip"
+    android:layout_height="wrap_content"
     android:orientation="vertical" >
     
     <TextView
@@ -22,5 +22,7 @@
         android:id="@+id/add_to_bookbag_button"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
-        android:text="@string/add_button"/>
+        android:text="@string/add_button"
+        android:layout_marginBottom="10dip"
+        />
 </LinearLayout>
index 79dfb7d..e541776 100644 (file)
 <?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="fill_parent"
-    android:layout_height="fill_parent">
+    android:layout_height="fill_parent" >
 
     <RelativeLayout
         android:id="@+id/header"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
-        android:orientation="horizontal"
         android:background="@drawable/header_rounded_corners"
+        android:orientation="horizontal"
         android:paddingTop="3dip"
-        >
-        
-        <ImageButton 
+        android:paddingBottom="2dip"
+         >
+
+        <ImageButton
             android:id="@+id/library_logo"
+            style="@style/LibraryLogoText"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
-            style="@style/LibraryLogoText"
             android:layout_alignParentLeft="true"
-            android:src="@drawable/evergreen_launcher_icon_48"
             android:layout_marginLeft="5dip"
-            />
+            android:src="@drawable/evergreen_launcher_icon_48" />
 
         <Button
             android:id="@+id/my_acount_button"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
-            android:text="@string/my_account_button_text"
             android:layout_alignParentRight="true"
-            android:padding="10dip"
             android:layout_marginTop="5dip"
-            />
+            android:padding="10dip"
+            android:text="@string/my_account_button_text" />
+    </RelativeLayout>
 
+    <LinearLayout
+        android:id="@+id/search_text_container"
+        android:layout_width="fill_parent"
+        android:layout_height="wrap_content"
+        android:layout_below="@id/header"
+        android:focusable="true"
+        android:focusableInTouchMode="true"
+        android:orientation="vertical"
+        android:padding="4dp"
+        android:background="@drawable/grey_rounded_corners"
+        android:layout_marginTop="4dip" 
+        >
 
-        <LinearLayout
-            android:orientation="horizontal"
+        <EditText
+            android:id="@+id/searchText"
             android:layout_width="fill_parent"
             android:layout_height="wrap_content"
-            android:padding="4dp"
-            android:id="@+id/search_text_container"
-            android:focusable="true" 
-               android:focusableInTouchMode="true"
-            android:layout_below="@id/library_logo">
-
-            <EditText
-                    android:id="@+id/searchText"
-                    android:layout_width="0dip"
-                    android:layout_height="wrap_content"
-                    android:hint="@string/search_hint"
-                    android:singleLine="true"
-                    android:layout_weight="1"/>
+            android:hint="@string/search_hint"
+            android:singleLine="true" />
+        
+        <LinearLayout
+        android:layout_width="fill_parent"
+        android:layout_height="wrap_content"
+        android:orientation="horizontal"
+        >
+
+        <Spinner
+            android:id="@+id/chose_organisation"
+            android:layout_width="0dp"
+            android:layout_weight="1"
+            android:layout_height="wrap_content"
+            android:layout_gravity="center_horizontal"
+               />
+
+        <ImageButton
+            android:id="@+id/searchButton"
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:paddingBottom="5dp"
+            android:paddingLeft="10dp"
+            android:paddingRight="10dp"
+            android:paddingTop="5dp"
+            android:src="@drawable/action_search" />
+    </LinearLayout>
+    
+    </LinearLayout>
 
+    
+
+    <LinearLayout
+        android:id="@+id/search_preference_options"
+        android:layout_width="fill_parent"
+        android:layout_height="wrap_content"
+        android:layout_below="@id/search_text_container"
+        android:layout_centerHorizontal="true"
+        android:layout_marginTop="5dip"
+        android:gravity="center"
+        android:orientation="vertical"
+         >
+
+        <LinearLayout
+            android:layout_width="fill_parent"
+            android:layout_height="wrap_content" >
+
+            <Button
+                android:id="@+id/menu_advanced_search_button"
+                style="@style/MenuListButton"
+                android:layout_width="150dip"
+                android:layout_height="wrap_content"
+                android:drawableTop="@drawable/settings_small"
+                android:text="@string/menu_button_advanced_search" />
+
+            <Button
+                android:id="@+id/library_hours_button"
+                style="@style/MenuListButton"
+                android:layout_width="150dip"
+                android:layout_height="wrap_content"
+                android:drawableTop="@drawable/settings_small"
+                android:text="@string/menu_button_library_hours" />
         </LinearLayout>
-        
-        <LinearLayout 
-            android:id="@+id/search_functions"
+
+        <LinearLayout
             android:layout_width="fill_parent"
             android:layout_height="wrap_content"
-            android:layout_below="@id/search_text_container">
-            
-                <Spinner 
-                               android:id="@+id/chose_organisation"   
-                               android:layout_width="fill_parent"
-                               android:layout_height="wrap_content"
-                               android:layout_gravity="center_horizontal"
-                               android:layout_weight="1"/>
-               
-                <ImageButton
-                    android:id="@+id/searchButton"
-                    android:layout_width="wrap_content"
-                    android:layout_height="wrap_content"
-                    android:src="@drawable/action_search"
-
-                    android:paddingLeft="10dp"
-                    android:paddingRight="10dp"
-                    android:paddingTop="5dp"
-                    android:paddingBottom="5dp"/> 
+            android:gravity="center" >
 
+            <Button
+                android:id="@+id/preference_button"
+                style="@style/MenuListButton"
+                android:layout_width="150dip"
+                android:layout_height="wrap_content"
+                android:drawableTop="@drawable/settings_small"
+                android:text="@string/menu_button_application_preferences" />
         </LinearLayout>
+    </LinearLayout>
 
-    </RelativeLayout>
+    <LinearLayout
+        android:id="@+id/search_text"
+        android:layout_width="fill_parent"
+        android:layout_height="wrap_content"
+        android:layout_below="@id/search_preference_options" >
+
+        <TextView
+            android:id="@+id/search_result_number"
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:textStyle="bold" />
+    </LinearLayout>
 
+    <ListView
+        android:id="@+id/search_results_list"
+        android:layout_width="fill_parent"
+        android:layout_height="fill_parent"
+        android:layout_below="@id/search_text" >
+    </ListView>
 
-     
-     <LinearLayout 
-         android:id="@+id/search_preference_options"
-         android:layout_width="250dip"
-         android:layout_height="wrap_content"
-         android:orientation="vertical"
-         android:gravity="center"
-         android:layout_below="@id/header"
-         android:layout_centerHorizontal="true"
-         >
-         
-         
-               <Button 
-                android:id="@+id/menu_advanced_search_button"
-             android:layout_width="fill_parent"
-             android:layout_height="wrap_content"
-             android:text="@string/menu_button_advanced_search"
-             android:drawableLeft="@drawable/settings_small"
-             style="@style/MenuListButton"
-             />
-         
-            <Button 
-             android:id="@+id/library_hours_button"
-             android:layout_width="fill_parent"
-             android:layout_height="wrap_content"
-             android:text="@string/menu_button_library_hours"
-             android:drawableLeft="@drawable/settings_small"
-             style="@style/MenuListButton"
-             />
-                  
-            
-            <Button
-             android:id="@+id/preference_button" 
-             android:layout_width="fill_parent"
-             android:layout_height="wrap_content"
-             android:text="@string/menu_button_application_preferences"
-             android:drawableLeft="@drawable/settings_small"
-             style="@style/MenuListButton"
-             />
-            
-     </LinearLayout>
-          <LinearLayout android:layout_width="fill_parent"
-         android:layout_height="wrap_content"
-         android:id="@+id/search_text"
-         android:layout_below="@id/search_preference_options"
-         >
-         <TextView 
-             android:id="@+id/search_result_number"
-             android:layout_width="wrap_content"
-             android:layout_height="wrap_content"
-             android:textStyle="bold"
-             />
-         
-     </LinearLayout>
-     
-       <ListView 
-         android:id="@+id/search_results_list"
-         android:layout_width="fill_parent"
-         android:layout_height="fill_parent"
-         android:layout_below="@id/search_text"  
-         ></ListView>
 </RelativeLayout>
\ No newline at end of file
index 215a258..54123d7 100644 (file)
     
     
     <!-- Menu Buttons -->
-    <color name="pres1">#de9d00</color>
-    <color name="pres2">#de4400</color>
+    <color name="pres1">#2AD652</color>
+    <color name="pres2">#44EA81</color>
     <color name="stro3">#eee</color>
     <color name="focu1">#000</color>
     <color name="focu2">#000</color>
     <color name="stro2">#fff</color>
-    <color name="norm1">#0068de</color>
-    <color name="norm2">#00ccde</color>
+    <color name="norm1">#8FD2D8</color>
+    <color name="norm2">#88E2DF</color>
     
        <!-- Header portion -->
     <color name="header_gradient_start">#E0F6E0</color>
-    <color name="header_gradient_stop">#6F9669</color>
+    <color name="grey">#ECECEC</color>
+    
+    <color name="header_gradient_stop">#1E7152</color>
     <color name="library_logo_text_coor">#000</color>
     
     
index dd62183..d7f7430 100644 (file)
     <!--  Menu List Buttons -->
    <style name="MenuListButton">
                <item name="android:background">@drawable/menu_button</item>
-               <item name="android:layout_marginTop">10dip</item>
-    </style>
-    
+               <item name="android:layout_margin">5dip</item>
+   </style>
     
+   <style name="DashboardButton">
+        <item name="android:layout_gravity">center_vertical</item>
+        <item name="android:layout_width">wrap_content</item>
+        <item name="android:layout_height">wrap_content</item>
+        <item name="android:gravity">center_horizontal</item>
+        <item name="android:drawablePadding">2dp</item>
+        <item name="android:textSize">16dp</item>
+        <item name="android:textStyle">bold</item>
+        <item name="android:textColor">#ff29549f</item>
+        <item name="android:background">@null</item>
+    </style> 
+   
+   
        <!-- Header portion start -->
        
        <style name="LibraryLogoText">
index 25d3e4d..c2bd9c5 100644 (file)
@@ -381,7 +381,12 @@ public class SearchCatalogListView extends Activity{
                                intent.putExtra("orgID",search.selectedOrganization.id);
                                intent.putExtra("depth",(search.selectedOrganization.level-1));
                                
+                               if(recordList.get(recordList.size()-1).dummy == true){
+                                       recordList.remove(recordList.size()-1);
+                               }
+                               
                                intent.putExtra("recordList", recordList);
+                               //TODO put total number
                                intent.putExtra("recordPosition", position);
                                startActivity(intent);
                        }
index 92c304b..bc8a9c3 100644 (file)
@@ -36,14 +36,12 @@ public abstract class BaseSampleActivity extends FragmentActivity {
 
             case R.id.add_page:
                 if (mAdapter.getCount() < 10) {
-                    mAdapter.setCount(mAdapter.getCount() + 1);
                     mIndicator.notifyDataSetChanged();
                 }
                 return true;
 
             case R.id.remove_page:
                 if (mAdapter.getCount() > 1) {
-                    mAdapter.setCount(mAdapter.getCount() - 1);
                     mIndicator.notifyDataSetChanged();
                 }
                 return true;
index 48dcd58..2a10eea 100644 (file)
@@ -101,8 +101,11 @@ public class BasicDetailsFragment extends Fragment{
            @Override
            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
-               if(savedInstanceState != null)
+               if(savedInstanceState != null){
                        record = (RecordInfo) savedInstanceState.getSerializable("recordInfo");
+                       this.position = savedInstanceState.getInt("position");
+                       this.total = savedInstanceState.getInt("total");
+               }
            }
            
            @Override
@@ -243,8 +246,8 @@ public class BasicDetailsFragment extends Fragment{
                         current_org = search.selectedOrganization.id;
                        
                        System.out.println("Size " + record.copyCountListInfo.size());
+                       
                        for(int i=0;i<record.copyCountListInfo.size();i++){
-                               //TODO
                                System.out.println(current_org + " " + record.copyCountListInfo.get(i).org_id + " " + record.copyCountListInfo.get(i).count);
                                if(record.copyCountListInfo.get(i).org_id == current_org){
                                        int total = record.copyCountListInfo.get(i).count;
@@ -287,6 +290,8 @@ public class BasicDetailsFragment extends Fragment{
            @Override
            public void onSaveInstanceState(Bundle outState) {
                outState.putSerializable("recordInfo", record);
+               outState.putInt("position", this.position);
+               outState.putInt("total", this.total);
                super.onSaveInstanceState(outState);
            }
            
diff --git a/Open-ILS/src/Android/src/org/evergreen/android/utils/ui/Dashboard.java b/Open-ILS/src/Android/src/org/evergreen/android/utils/ui/Dashboard.java
new file mode 100644 (file)
index 0000000..126e827
--- /dev/null
@@ -0,0 +1,174 @@
+package org.evergreen.android.utils.ui;
+
+import android.content.Context;
+import android.util.AttributeSet;
+import android.view.View;
+import android.view.ViewGroup;
+
+/**
+ * Custom layout that arranges children in a grid-like manner, optimizing for even horizontal and
+ * vertical whitespace.
+ */
+public class Dashboard extends ViewGroup {
+
+    private static final int UNEVEN_GRID_PENALTY_MULTIPLIER = 10;
+
+    private int mMaxChildWidth = 0;
+    private int mMaxChildHeight = 0;
+
+    public Dashboard(Context context) {
+        super(context, null);
+    }
+
+    public Dashboard(Context context, AttributeSet attrs) {
+        super(context, attrs, 0);
+    }
+
+    public Dashboard(Context context, AttributeSet attrs, int defStyle) {
+        super(context, attrs, defStyle);
+    }
+
+    @Override
+    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
+        mMaxChildWidth = 0;
+        mMaxChildHeight = 0;
+
+        // Measure once to find the maximum child size.
+
+        int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(
+                MeasureSpec.getSize(widthMeasureSpec), MeasureSpec.AT_MOST);
+        int childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(
+                MeasureSpec.getSize(widthMeasureSpec), MeasureSpec.AT_MOST);
+
+        final int count = getChildCount();
+        for (int i = 0; i < count; i++) {
+            final View child = getChildAt(i);
+            if (child.getVisibility() == GONE) {
+                continue;
+            }
+
+            child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
+
+            mMaxChildWidth = Math.max(mMaxChildWidth, child.getMeasuredWidth());
+            mMaxChildHeight = Math.max(mMaxChildHeight, child.getMeasuredHeight());
+        }
+
+        // Measure again for each child to be exactly the same size.
+
+        childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(
+                mMaxChildWidth, MeasureSpec.EXACTLY);
+        childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(
+                mMaxChildHeight, MeasureSpec.EXACTLY);
+
+        for (int i = 0; i < count; i++) {
+            final View child = getChildAt(i);
+            if (child.getVisibility() == GONE) {
+                continue;
+            }
+
+            child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
+        }
+
+        setMeasuredDimension(
+                resolveSize(mMaxChildWidth, widthMeasureSpec),
+                resolveSize(mMaxChildHeight, heightMeasureSpec));
+    }
+
+    @Override
+    protected void onLayout(boolean changed, int l, int t, int r, int b) {
+        int width = r - l;
+        int height = b - t;
+
+        final int count = getChildCount();
+
+        // Calculate the number of visible children.
+        int visibleCount = 0;
+        for (int i = 0; i < count; i++) {
+            final View child = getChildAt(i);
+            if (child.getVisibility() == GONE) {
+                continue;
+            }
+            ++visibleCount;
+        }
+
+        if (visibleCount == 0) {
+            return;
+        }
+
+        // Calculate what number of rows and columns will optimize for even horizontal and
+        // vertical whitespace between items. Start with a 1 x N grid, then try 2 x N, and so on.
+        int bestSpaceDifference = Integer.MAX_VALUE;
+        int spaceDifference;
+
+        // Horizontal and vertical space between items
+        int hSpace = 0;
+        int vSpace = 0;
+
+        int cols = 1;
+        int rows;
+
+        while (true) {
+            rows = (visibleCount - 1) / cols + 1;
+
+            hSpace = ((width - mMaxChildWidth * cols) / (cols + 1));
+            vSpace = ((height - mMaxChildHeight * rows) / (rows + 1));
+
+            spaceDifference = Math.abs(vSpace - hSpace);
+            if (rows * cols != visibleCount) {
+                spaceDifference *= UNEVEN_GRID_PENALTY_MULTIPLIER;
+            }
+
+            if (spaceDifference < bestSpaceDifference) {
+                // Found a better whitespace squareness/ratio
+                bestSpaceDifference = spaceDifference;
+
+                // If we found a better whitespace squareness and there's only 1 row, this is
+                // the best we can do.
+                if (rows == 1) {
+                    break;
+                }
+            } else {
+                // This is a worse whitespace ratio, use the previous value of cols and exit.
+                --cols;
+                rows = (visibleCount - 1) / cols + 1;
+                hSpace = ((width - mMaxChildWidth * cols) / (cols + 1));
+                vSpace = ((height - mMaxChildHeight * rows) / (rows + 1));
+                break;
+            }
+
+            ++cols;
+        }
+
+        // Lay out children based on calculated best-fit number of rows and cols.
+
+        // If we chose a layout that has negative horizontal or vertical space, force it to zero.
+        hSpace = Math.max(0, hSpace);
+        vSpace = Math.max(0, vSpace);
+
+        // Re-use width/height variables to be child width/height.
+        width = (width - hSpace * (cols + 1)) / cols;
+        height = (height - vSpace * (rows + 1)) / rows;
+
+        int left, top;
+        int col, row;
+        int visibleIndex = 0;
+        for (int i = 0; i < count; i++) {
+            final View child = getChildAt(i);
+            if (child.getVisibility() == GONE) {
+                continue;
+            }
+
+            row = visibleIndex / cols;
+            col = visibleIndex % cols;
+
+            left = hSpace * (col + 1) + width * col;
+            top = vSpace * (row + 1) + height * row;
+
+            child.layout(left, top,
+                    (hSpace == 0 && col == cols - 1) ? r : (left + width),
+                    (vSpace == 0 && row == rows - 1) ? b : (top + height));
+            ++visibleIndex;
+        }
+    }
+}
+
index c898752..ea62a40 100644 (file)
@@ -23,10 +23,4 @@ public class TestFragmentAdapter extends FragmentPagerAdapter {
         return mCount;\r
     }\r
 \r
-    public void setCount(int count) {\r
-        if (count > 0 && count <= 10) {\r
-            mCount = count;\r
-            notifyDataSetChanged();\r
-        }\r
-    }\r
 }
\ No newline at end of file