ソースを参照

1:修改BaseActivity,增加BaseFragment;

guiying712 8 年 前
コミット
42f6be0cca

+ 60 - 0
lib_common/src/main/java/com/guiying/common/base/BaseActivity.java

@@ -8,6 +8,7 @@ import android.support.v7.widget.Toolbar;
 import android.view.View;
 
 import com.guiying.common.R;
+import com.guiying.common.utils.Utils;
 
 /**
  * <p>Activity基类 </p>
@@ -66,4 +67,63 @@ public abstract class BaseActivity extends AppCompatActivity {
         return true;
     }
 
+    //添加fragment
+    protected void addFragment(BaseFragment fragment, @IdRes int frameId) {
+        Utils.checkNotNull(fragment);
+        getSupportFragmentManager().beginTransaction()
+                .add(frameId, fragment, fragment.getClass().getSimpleName())
+                .addToBackStack(fragment.getClass().getSimpleName())
+                .commitAllowingStateLoss();
+
+    }
+
+    //替换fragment
+    protected void replaceFragment(BaseFragment fragment, @IdRes int frameId) {
+        Utils.checkNotNull(fragment);
+        getSupportFragmentManager().beginTransaction()
+                .replace(frameId, fragment, fragment.getClass().getSimpleName())
+                .addToBackStack(fragment.getClass().getSimpleName())
+                .commitAllowingStateLoss();
+
+    }
+
+    //隐藏fragment
+    protected void hideFragment(BaseFragment fragment) {
+        Utils.checkNotNull(fragment);
+        getSupportFragmentManager().beginTransaction()
+                .hide(fragment)
+                .commitAllowingStateLoss();
+
+    }
+
+
+    //显示fragment
+    protected void showFragment(BaseFragment fragment) {
+        Utils.checkNotNull(fragment);
+        getSupportFragmentManager().beginTransaction()
+                .show(fragment)
+                .commitAllowingStateLoss();
+
+    }
+
+
+    protected void removeFragment(BaseFragment fragment) {
+        Utils.checkNotNull(fragment);
+        getSupportFragmentManager().beginTransaction()
+                .remove(fragment)
+                .commitAllowingStateLoss();
+
+    }
+
+
+    //移除fragment
+    protected void popFragment() {
+        if (getSupportFragmentManager().getBackStackEntryCount() > 1) {
+            getSupportFragmentManager().popBackStack();
+        } else {
+            finish();
+        }
+    }
+
+
 }

+ 65 - 0
lib_common/src/main/java/com/guiying/common/base/BaseFragment.java

@@ -0,0 +1,65 @@
+package com.guiying.common.base;
+
+import android.content.Context;
+import android.support.annotation.IdRes;
+import android.support.v4.app.Fragment;
+
+import com.guiying.common.utils.Utils;
+
+public abstract class BaseFragment extends Fragment {
+
+    protected BaseActivity mActivity;
+
+
+    //获取宿主Activity
+    protected BaseActivity getHoldingActivity() {
+        return mActivity;
+    }
+
+    @Override
+    public void onAttach(Context context) {
+        super.onAttach(context);
+        this.mActivity = (BaseActivity) context;
+    }
+
+
+    //添加fragment
+    protected void addFragment(BaseFragment fragment, @IdRes int frameId) {
+        Utils.checkNotNull(fragment);
+        getHoldingActivity().addFragment(fragment, frameId);
+
+    }
+
+    //替换fragment
+    protected void replaceFragment(BaseFragment fragment, @IdRes int frameId) {
+        Utils.checkNotNull(fragment);
+        getHoldingActivity().replaceFragment(fragment, frameId);
+    }
+
+    //隐藏fragment
+    protected void hideFragment(BaseFragment fragment) {
+        Utils.checkNotNull(fragment);
+        getHoldingActivity().hideFragment(fragment);
+    }
+
+
+    //显示fragment
+    protected void showFragment(BaseFragment fragment) {
+        Utils.checkNotNull(fragment);
+        getHoldingActivity().showFragment(fragment);
+    }
+
+
+    protected void removeFragment(BaseFragment fragment) {
+        Utils.checkNotNull(fragment);
+        getHoldingActivity().removeFragment(fragment);
+
+    }
+
+
+    //移除fragment
+    protected void popFragment() {
+        getHoldingActivity().popFragment();
+    }
+
+}

+ 26 - 0
lib_common/src/main/java/com/guiying/common/utils/Utils.java

@@ -1,5 +1,6 @@
 package com.guiying.common.utils;
 
+
 import android.app.Activity;
 import android.content.Context;
 import android.content.ContextWrapper;
@@ -7,6 +8,9 @@ import android.content.pm.ApplicationInfo;
 import android.content.pm.PackageManager;
 import android.support.annotation.NonNull;
 import android.support.annotation.StringRes;
+import android.support.v4.app.Fragment;
+import android.support.v4.app.FragmentManager;
+import android.support.v4.app.FragmentTransaction;
 import android.view.View;
 
 /**
@@ -87,4 +91,26 @@ public class Utils {
         }
     }
 
+
+    /**
+     * The {@code fragment} is added to the container view with id {@code frameId}. The operation is
+     * performed by the {@code fragmentManager}.
+     */
+    public static void addFragmentToActivity(@NonNull FragmentManager fragmentManager,
+                                             @NonNull Fragment fragment, int frameId) {
+        checkNotNull(fragmentManager);
+        checkNotNull(fragment);
+        FragmentTransaction transaction = fragmentManager.beginTransaction();
+        transaction.add(frameId, fragment);
+        transaction.commit();
+    }
+
+
+    public static <T> T checkNotNull(T obj) {
+        if (obj == null) {
+            throw new NullPointerException();
+        }
+        return obj;
+    }
+
 }