Java Question Problem with ViewPager library

corwin42

Expert
Licensed User
Longtime User
Hi,

I'm trying to add a chart to a ViewPager and I can't get it to work.

The problem is that the Panels I add to the PageAdapter (AHPageContainer in B4A) are added to a ViewPager object (which is extended from ViewGroup) and the LayoutParams() are not set. So in the B4A Debugger I can see that my panel is shown with "Layout not available" and it is not possible to get the Canvas from this object because it needs the LayoutParams to be set.

I have tried to set the LayoutParams to the Panel when it is added to the ViewPager object. This does not work because then I get a ClassCastException (see below) when ViewPager.onMeasure gets called.

B4X:
java.lang.ClassCastException: anywheresoftware.b4a.BALayout$LayoutParams
   at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1027)
   at android.view.View.measure(View.java:8313)
   at android.view.ViewGroup.measureChild(ViewGroup.java:3109)
   at android.view.ViewGroup.measureChildren(ViewGroup.java:3086)
   at anywheresoftware.b4a.BALayout.onMeasure(BALayout.java:45)
   at android.view.View.measure(View.java:8313)
   at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
   at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
   at android.view.View.measure(View.java:8313)
   at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
   at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
   at android.view.View.measure(View.java:8313)
   at android.view.ViewRoot.performTraversals(ViewRoot.java:839)
   at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
   at android.os.Handler.dispatchMessage(Handler.java:99)
   at android.os.Looper.loop(Looper.java:130)
   at android.app.ActivityThread.main(ActivityThread.java:3683)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:507)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
   at dalvik.system.NativeStart.main(Native Method)

Any Ideas how I can solve this problem?

Here is the current code of the Page added to the ViewPager:
(LayoutParams is of type BALayout.LayoutParams.

B4X:
   public Object instantiateItem(ViewGroup container, int position) {
      View v = (View) pages.Get(position);
      LayoutParams lp =  new LayoutParams(0, 0, container.getLayoutParams().width, container.getLayoutParams().height);
      container.addView(v,lp);
      v.setLayoutParams(lp);
      
      if (mBa.subExists(mEventName + "_pagecreated")) {
         mBa.raiseEvent(container, mEventName + "_pagecreated", new Object[] {position, v});
      }
      
      return v;
   }
 

corwin42

Expert
Licensed User
Longtime User
Solved.

The instantiateItem method now looks like this.

B4X:
   public Object instantiateItem(ViewGroup container, int position) {
      View v = (View) pages.Get(position);
      LayoutParams lp =  new LayoutParams();
      lp.width = container.getLayoutParams().width;
      lp.height = container.getLayoutParams().height;
      ((ViewPager) container).addView(v,0,lp);
      v.setLayoutParams(lp);
      
      if (mBa.subExists(mEventName + "_pagecreated")) {
         mBa.raiseEvent(container, mEventName + "_pagecreated", new Object[] {position, v});
      }
      
      return v;
   }

LayoutParams is of type ViewPager.LayoutParams. The trick was to set the width and height manually so the canvas can be created.

Sometimes I have a knot in the brain :BangHead:. This thing took me two evenings to fix.
 
Top