Keep controllers attached but hide them, instead of removing the view

Previously when a controller because hidden, it was removed from the view hierachy, and reattached when shown again.
Now the views are only detached from the view hierachy when the whole controller gets destroyed, and the view visibility is changed on onHide and onShow.
This was done for performance reasons. Reattaching a controller takes too much time and is noticable when trying to swipe a controller away.
multisite
Floens 10 years ago
parent cd1a9991bb
commit 430c0963bd
  1. 60
      Clover/app/src/main/java/org/floens/chan/controller/Controller.java
  2. 6
      Clover/app/src/main/java/org/floens/chan/controller/NavigationController.java
  3. 2
      Clover/app/src/main/java/org/floens/chan/ui/controller/DrawerController.java
  4. 2
      Clover/app/src/main/java/org/floens/chan/ui/controller/PopupController.java
  5. 8
      Clover/app/src/main/java/org/floens/chan/ui/controller/SplitNavigationController.java

@ -21,6 +21,7 @@ import android.content.Context;
import android.content.res.Configuration; import android.content.res.Configuration;
import android.view.KeyEvent; import android.view.KeyEvent;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import org.floens.chan.controller.transition.FadeInTransition; import org.floens.chan.controller.transition.FadeInTransition;
@ -82,6 +83,8 @@ public abstract class Controller {
Logger.test(getClass().getSimpleName() + " onShow"); Logger.test(getClass().getSimpleName() + " onShow");
} }
view.setVisibility(View.VISIBLE);
for (Controller controller : childControllers) { for (Controller controller : childControllers) {
if (!controller.shown) { if (!controller.shown) {
controller.onShow(); controller.onShow();
@ -95,6 +98,8 @@ public abstract class Controller {
Logger.test(getClass().getSimpleName() + " onHide"); Logger.test(getClass().getSimpleName() + " onHide");
} }
view.setVisibility(View.GONE);
for (Controller controller : childControllers) { for (Controller controller : childControllers) {
if (controller.shown) { if (controller.shown) {
controller.onHide(); controller.onHide();
@ -111,6 +116,10 @@ public abstract class Controller {
while (childControllers.size() > 0) { while (childControllers.size() > 0) {
removeChildController(childControllers.get(0)); removeChildController(childControllers.get(0));
} }
if (AndroidUtils.removeFromParentView(view)) {
Logger.test(getClass().getSimpleName() + " view removed onDestroy");
}
} }
public void addChildController(Controller controller) { public void addChildController(Controller controller) {
@ -125,29 +134,22 @@ public abstract class Controller {
return childControllers.remove(controller); return childControllers.remove(controller);
} }
public void attach(ViewGroup parentView, boolean over) { public void attachToParentView(ViewGroup parentView, boolean over) {
ViewGroup.LayoutParams params = view.getLayoutParams(); if (view.getParent() != null) {
if (LOG_STATES) {
if (params == null) { Logger.test(getClass().getSimpleName() + " view removed");
params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); }
} else { AndroidUtils.removeFromParentView(view);
params.width = ViewGroup.LayoutParams.MATCH_PARENT;
params.height = ViewGroup.LayoutParams.MATCH_PARENT;
} }
view.setLayoutParams(params); if (parentView != null) {
if (LOG_STATES) {
if (over) { Logger.test(getClass().getSimpleName() + " view attached");
parentView.addView(view, view.getLayoutParams()); }
} else { attachToView(parentView, over);
parentView.addView(view, 0, view.getLayoutParams());
} }
} }
public void detach() {
AndroidUtils.removeFromParentView(view);
}
public void onConfigurationChanged(Configuration newConfig) { public void onConfigurationChanged(Configuration newConfig) {
for (Controller controller : childControllers) { for (Controller controller : childControllers) {
controller.onConfigurationChanged(newConfig); controller.onConfigurationChanged(newConfig);
@ -186,7 +188,7 @@ public abstract class Controller {
controller.presentingByController = this; controller.presentingByController = this;
controller.onCreate(); controller.onCreate();
controller.attach(contentView, true); controller.attachToView(contentView, true);
controller.onShow(); controller.onShow();
if (animated) { if (animated) {
@ -223,7 +225,6 @@ public abstract class Controller {
private void finishPresenting() { private void finishPresenting() {
onHide(); onHide();
detach();
onDestroy(); onDestroy();
} }
@ -238,4 +239,23 @@ public abstract class Controller {
public ViewGroup inflateRes(int resId) { public ViewGroup inflateRes(int resId) {
return (ViewGroup) LayoutInflater.from(context).inflate(resId, null); return (ViewGroup) LayoutInflater.from(context).inflate(resId, null);
} }
private void attachToView(ViewGroup parentView, boolean over) {
ViewGroup.LayoutParams params = view.getLayoutParams();
if (params == null) {
params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
} else {
params.width = ViewGroup.LayoutParams.MATCH_PARENT;
params.height = ViewGroup.LayoutParams.MATCH_PARENT;
}
view.setLayoutParams(params);
if (over) {
parentView.addView(view, view.getLayoutParams());
} else {
parentView.addView(view, 0, view.getLayoutParams());
}
}
} }

@ -89,7 +89,6 @@ public abstract class NavigationController extends Controller {
throw new IllegalArgumentException("Cannot transition while another transition is in progress."); throw new IllegalArgumentException("Cannot transition while another transition is in progress.");
} }
to.attach(container, false);
to.onShow(); to.onShow();
return true; return true;
@ -101,11 +100,9 @@ public abstract class NavigationController extends Controller {
public void endSwipeTransition(final Controller from, final Controller to, boolean finish) { public void endSwipeTransition(final Controller from, final Controller to, boolean finish) {
if (finish) { if (finish) {
from.onHide(); from.onHide();
from.detach();
removeChildController(from); removeChildController(from);
} else { } else {
to.onHide(); to.onHide();
to.detach();
} }
controllerTransition = null; controllerTransition = null;
@ -126,7 +123,7 @@ public abstract class NavigationController extends Controller {
} }
if (to != null) { if (to != null) {
to.attach(container, pushing); to.attachToParentView(container, pushing);
to.onShow(); to.onShow();
} }
@ -158,7 +155,6 @@ public abstract class NavigationController extends Controller {
private void finishTransition(Controller from, boolean pushing) { private void finishTransition(Controller from, boolean pushing) {
if (from != null) { if (from != null) {
from.onHide(); from.onHide();
from.detach();
} }
if (!pushing && from != null) { if (!pushing && from != null) {

@ -115,7 +115,7 @@ public class DrawerController extends Controller implements PinAdapter.Callback,
public void setChildController(Controller childController) { public void setChildController(Controller childController) {
addChildController(childController); addChildController(childController);
childController.attach(container, true); childController.attachToParentView(container, true);
childController.onShow(); childController.onShow();
} }

@ -45,7 +45,7 @@ public class PopupController extends Controller implements View.OnClickListener
public void setChildController(NavigationController childController) { public void setChildController(NavigationController childController) {
addChildController(childController); addChildController(childController);
childController.attach(container, true); childController.attachToParentView(container, true);
childController.onShow(); childController.onShow();
} }

@ -82,7 +82,6 @@ public class SplitNavigationController extends Controller implements AndroidUtil
public void setLeftController(Controller leftController) { public void setLeftController(Controller leftController) {
if (this.leftController != null) { if (this.leftController != null) {
this.leftController.onHide(); this.leftController.onHide();
this.leftController.detach();
removeChildController(this.leftController); removeChildController(this.leftController);
} }
@ -90,17 +89,14 @@ public class SplitNavigationController extends Controller implements AndroidUtil
if (leftController != null) { if (leftController != null) {
addChildController(leftController); addChildController(leftController);
leftController.attach(leftControllerView, true); leftController.attachToParentView(leftControllerView, true);
leftController.onShow(); leftController.onShow();
} else {
} }
} }
public void setRightController(Controller rightController) { public void setRightController(Controller rightController) {
if (this.rightController != null) { if (this.rightController != null) {
this.rightController.onHide(); this.rightController.onHide();
this.rightController.detach();
removeChildController(this.rightController); removeChildController(this.rightController);
} else { } else {
rightControllerView.removeAllViews(); rightControllerView.removeAllViews();
@ -110,7 +106,7 @@ public class SplitNavigationController extends Controller implements AndroidUtil
if (rightController != null) { if (rightController != null) {
addChildController(rightController); addChildController(rightController);
rightController.attach(rightControllerView, true); rightController.attachToParentView(rightControllerView, true);
rightController.onShow(); rightController.onShow();
} else { } else {
rightControllerView.addView(emptyView); rightControllerView.addView(emptyView);

Loading…
Cancel
Save