OSDN Git Service

backstack fixes
authorKoushik Dutta <koushd@gmail.com>
Thu, 28 Feb 2013 01:23:16 +0000 (17:23 -0800)
committerKoushik Dutta <koushd@gmail.com>
Thu, 28 Feb 2013 01:23:16 +0000 (17:23 -0800)
Widgets/res/layout/list_content.xml
Widgets/src/com/koushikdutta/widgets/BetterListFragmentInternal.java
Widgets/src/com/koushikdutta/widgets/FragmentInterfaceWrapper.java
Widgets/src/com/koushikdutta/widgets/ListContentFragmentInternal.java

index d38d2ea..5678ade 100644 (file)
@@ -1,23 +1,9 @@
 <?xml version="1.0" encoding="utf-8"?>
-<ViewSwitcher xmlns:android="http://schemas.android.com/apk/res/android"
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:id="@+id/list_content_container"
     android:layout_width="fill_parent"
-    android:layout_height="fill_parent"
-    android:inAnimation="@android:anim/slide_in_left"
-    android:outAnimation="@android:anim/slide_out_right" >
+    android:layout_height="fill_parent" >
 
-    <LinearLayout
-        android:layout_width="fill_parent"
-        android:layout_height="fill_parent"
-        android:orientation="vertical" >
+    <include layout="@layout/list_fragment" android:id="@+id/list_fragment" />
 
-        <include layout="@layout/list_fragment" />
-    </LinearLayout>
-
-    <LinearLayout
-        android:id="@+id/content"
-        android:layout_width="fill_parent"
-        android:layout_height="fill_parent"
-        android:orientation="vertical" />
-
-</ViewSwitcher>
\ No newline at end of file
+</LinearLayout>
\ No newline at end of file
index 2854b48..15037a2 100644 (file)
@@ -2,6 +2,8 @@ package com.koushikdutta.widgets;
 
 import java.util.Comparator;
 
+import junit.framework.Assert;
+
 import android.content.Context;
 import android.content.res.Configuration;
 import android.os.Bundle;
@@ -308,8 +310,15 @@ public class BetterListFragmentInternal extends FragmentInterface {
         setPadding();
     }
 
+    int containerId;
+    public int getContainerId() {
+        return containerId;
+    }
+    
     @Override
     public final View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
+        containerId = container.getId();
+        Assert.assertTrue(containerId > 0);
         View ret = inflater.inflate(getListFragmentResource(), container, false);
 
         mTitleContainer = (ViewGroup)ret.findViewById(R.id.title_container);
index 01e7fa3..f3fbb33 100644 (file)
@@ -2,6 +2,7 @@ package com.koushikdutta.widgets;
 
 import android.app.Activity;
 import android.os.Bundle;
+import android.view.View;
 
 public interface FragmentInterfaceWrapper {
     public Activity getActivity();
@@ -10,4 +11,5 @@ public interface FragmentInterfaceWrapper {
     
     void setArguments(Bundle bundle);
     Bundle getArguments();
+    View getView();
 }
index 5ef28ef..339f4a1 100644 (file)
@@ -1,15 +1,17 @@
 package com.koushikdutta.widgets;
 
+import junit.framework.Assert;
 import android.app.Activity;
 import android.content.res.Configuration;
 import android.os.Bundle;
 import android.support.v4.app.Fragment;
 import android.support.v4.app.FragmentActivity;
+import android.support.v4.app.FragmentManager;
+import android.support.v4.app.FragmentManager.OnBackStackChangedListener;
 import android.support.v4.app.FragmentTransaction;
 import android.view.View;
 import android.view.ViewGroup;
 import android.widget.ListView;
-import android.widget.ViewSwitcher;
 
 public class ListContentFragmentInternal extends BetterListFragmentInternal {
     ViewGroup mContent;
@@ -51,20 +53,41 @@ public class ListContentFragmentInternal extends BetterListFragmentInternal {
     }
     
     public boolean isPaged() {
-        return mContainer instanceof ViewSwitcher;
+//        return mContainer instanceof ViewSwitcher;
+        return mContent == null;
+    }
+    
+    int getContentId() {
+        if (isPaged())
+            return R.id.list_content_container;
+        return R.id.content;
     }
     
     void setContentNative() {
         android.app.Fragment f = (android.app.Fragment)mCurrentContent;
         Activity fa = getActivity();
-        android.app.FragmentTransaction ft = fa.getFragmentManager().beginTransaction();
-        ft.replace(R.id.content, f);
-        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
-        if (mContainer instanceof ViewSwitcher) {
-            ViewSwitcher switcher = (ViewSwitcher)mContainer;
-            if (mContent != switcher.getCurrentView())
-                switcher.showNext();
+        final android.app.FragmentManager fm = fa.getFragmentManager();
+        android.app.FragmentTransaction ft = fm.beginTransaction();
+        if (isPaged()) {
+            final int curSize = fm.getBackStackEntryCount();
+            View v = getFragment().getView();
+            Assert.assertNotNull(v);
+            final View l = v.findViewById(R.id.list_fragment);
+            Assert.assertNotNull(l);
+            l.setVisibility(View.GONE);
+            fm.addOnBackStackChangedListener(new android.app.FragmentManager.OnBackStackChangedListener() {
+                @Override
+                public void onBackStackChanged() {
+                    if (curSize != fm.getBackStackEntryCount())
+                        return;
+                    l.setVisibility(View.VISIBLE);
+                    fm.removeOnBackStackChangedListener(this);
+                }
+            });
+            ft.addToBackStack("content");
         }
+        ft.replace(getContentId(), f, "content");
+        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
         ft.commit();
     }
     
@@ -73,14 +96,28 @@ public class ListContentFragmentInternal extends BetterListFragmentInternal {
         if (getActivity() instanceof FragmentActivity) {
             Fragment f = (Fragment)mCurrentContent;
             FragmentActivity fa = (FragmentActivity)getActivity();
+            final FragmentManager fm = fa.getSupportFragmentManager();
             FragmentTransaction ft = fa.getSupportFragmentManager().beginTransaction();
-            ft.replace(R.id.content, f);
-            ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
-            if (mContainer instanceof ViewSwitcher) {
-                ViewSwitcher switcher = (ViewSwitcher)mContainer;
-                if (mContent != switcher.getCurrentView())
-                    switcher.showNext();
+            if (isPaged()) {
+                final int curSize = fm.getBackStackEntryCount();
+                View v = getFragment().getView();
+                Assert.assertNotNull(v);
+                final View l = v.findViewById(R.id.list_fragment);
+                Assert.assertNotNull(l);
+                l.setVisibility(View.GONE);
+                fm.addOnBackStackChangedListener(new OnBackStackChangedListener() {
+                    @Override
+                    public void onBackStackChanged() {
+                        if (curSize != fm.getBackStackEntryCount())
+                            return;
+                        l.setVisibility(View.VISIBLE);
+                        fm.removeOnBackStackChangedListener(this);
+                    }
+                });
+                ft.addToBackStack("content");
             }
+            ft.replace(getContentId(), f, "content");
+            ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
             ft.commit();
         }
         else {
@@ -91,38 +128,6 @@ public class ListContentFragmentInternal extends BetterListFragmentInternal {
             getListView().clearChoices();
     }
 
-    public void goBackNative() {
-        android.app.Fragment f = (android.app.Fragment)mCurrentContent;
-        Activity fa = getActivity();
-        ((ViewSwitcher)mContainer).showPrevious();
-        fa.getFragmentManager().beginTransaction()
-        .remove(f)
-        .commit();
-        mCurrentContent = null;
-    }
-    
-    public boolean onBackPressed() {
-        if (mCurrentContent == null)
-            return false;
-        if (mContainer instanceof ViewSwitcher) {
-            if (getActivity() instanceof FragmentActivity) {
-                Fragment f = (Fragment)mCurrentContent;
-                FragmentActivity fa = (FragmentActivity)getActivity();
-                ((ViewSwitcher)mContainer).showPrevious();
-                fa.getSupportFragmentManager().beginTransaction()
-                .remove(f)
-                .commit();
-                mCurrentContent = null;
-                return true;
-            }
-            else {
-                goBackNative();
-                return true;
-            }
-        }
-        return false;
-    }
-
     @Override
     protected int getListItemResource() {
         return R.layout.list_item_selectable;
@@ -133,10 +138,6 @@ public class ListContentFragmentInternal extends BetterListFragmentInternal {
         return R.layout.list_content;
     }
     
-    public ViewGroup getContent() {
-        return mContent;
-    }
-    
     @Override
     public void onConfigurationChanged(Configuration newConfig) {
         super.onConfigurationChanged(newConfig);