Android Question Use both ViewWrapper and DesignerCustomView

warwound

Expert
Licensed User
Longtime User
If i wrap a View and the wrapper extends ViewWrapper and also implements DesignerCustomView then whereabouts in the wrapper do i create the wrapped View?

If the wrapper does not implement DesignerCustomView then i'd do something like:

B4X:
@BA.ActivityObject
@BA.ShortName("ScannerLiveView")
public class ScannerLiveViewWrapper extends ViewWrapper<ScannerLiveView>{

   @Override
   public void Initialize(BA ba, String EventName) {
       super.Initialize(ba, EventName);
   }

   @Override
   public void innerInitialize(BA ba, String eventName, boolean keepOldObject) {
       if(!keepOldObject){
           setObject(new ScannerLiveView(ba.context));
           keepOldObject=true;
       }
       super.innerInitialize(ba, eventName, keepOldObject);
   }
}

And if i was to implement DesignerCustomView but not extend ViewWrapper then:

B4X:
@BA.ActivityObject
@BA.ShortName("ScannerLiveView")
public class ScannerLiveViewWrapper implements Common.DesignerCustomView {

   private ScannerLiveView scannerLiveView;

   private void addToParent(ViewGroup viewGroup, @BA.Pixel int left, @BA.Pixel int top, @BA.Pixel int width, @BA.Pixel int height) {
       viewGroup.addView(scannerLiveView, new BALayout.LayoutParams(left, top, width, height));
   }

   @Override
   public void DesignerCreateView(PanelWrapper panelWrapper, LabelWrapper labelWrapper, Map map) {
       ViewGroup localViewGroup = (ViewGroup) panelWrapper.getObject().getParent();
       addToParent(localViewGroup, panelWrapper.getLeft(), panelWrapper.getTop(), panelWrapper.getWidth(), panelWrapper.getHeight());
       panelWrapper.RemoveView();
   }

   @BA.Hide
   @Override
   public void _initialize(BA ba, Object activityClass, String eventName) {
       scannerLiveView=new ScannerLiveView(ba.context);
   }
   
   public void Initialize(BA ba, String EventName) {
       _initialize(ba, null, EventName);
   }

}

Currently my class that extends ViewWrapper and implements DesignerCustomView is:

B4X:
@BA.ActivityObject
@BA.ShortName("ScannerLiveView")
public class ScannerLiveViewWrapper extends ViewWrapper<ScannerLiveView> implements Common.DesignerCustomView {

   private void addToParent(ViewGroup viewGroup, @BA.Pixel int left, @BA.Pixel int top, @BA.Pixel int width, @BA.Pixel int height) {
       viewGroup.addView(getObject(), new BALayout.LayoutParams(left, top, width, height));
   }

   @Override
   public void DesignerCreateView(PanelWrapper panelWrapper, LabelWrapper labelWrapper, Map map) {
       ViewGroup localViewGroup = (ViewGroup) panelWrapper.getObject().getParent();
       addToParent(localViewGroup, panelWrapper.getLeft(), panelWrapper.getTop(), panelWrapper.getWidth(), panelWrapper.getHeight());
       panelWrapper.RemoveView();
   }

   @BA.Hide
   @Override
       //   create new View here?
       setObject(new ScannerLiveView(ba.context));
   }

   @Override
   public void Initialize(BA ba, String EventName) {
       super.Initialize(ba, EventName);
   }

   @Override
   public void innerInitialize(BA ba, String eventName, boolean keepOldObject) {
       if(!keepOldObject){
           //   create new View here?
           setObject(new ScannerLiveView(ba.context));
           keepOldObject=true;
       }
       super.innerInitialize(ba, eventName, keepOldObject);
   }
}

My comments 'create new View here?' shows where i'm confused.
Whereabouts is best to create the new wrapped View?

Thanks.
 

warwound

Expert
Licensed User
Longtime User
I wouldn't extend from ViewWrapper and instead expose the base panel. It is simpler to implement and it is the same as B4X written custom views.

My only reason for extending ViewWrapper was to give my View the various methods and properties of ViewWrapper such as Top, Bottom, Width and Height.
If i can achieve the same by exposing the base panel then that will be good enough for me.

I'll experiment later and see how it works.

Thanks.
 
Upvote 0
Top