I use the viewpager in my application to dynamically load views and scroll them horizontally. I also used pageTransform to scale the next and previous views. Selected code from
http://developer.android.com/training/animation/screen-slide.html
Each of the following depends on the user’s responses to the previous view. In each OnPageSelected, I load the following view
mPagerAdapter.addView(ViewToBeAdded, AddAtPostion);
Problem Step 1: As soon as I do the setAdapter, the entire viewpager scrolls to the first position (I'm not sure why this is happening).
Problem Step 2: To work around problem 1, I called
mViewPager.setCurrentItem(AddAtPostion, false);.
// If I call with the second parameter as true, the viewPager scrolls from the first element to the current element.
However, when I call it, the transforms set using pageTransform are deleted (since the recipient is applied only when scrolling), and the previous view holder is displayed without any transform.
Problem Step 3: To work around problem 2, I added runnable to invoke fake drag and drop after setCurrentItem (as extracted from applied PageTransformer to PagerView as soon as possible )
private Handler mhandler = new Handler(); private Runnable mrunnable = new Runnable() { public void run() { mViewPager.beginFakeDrag(); mViewPager.fakeDragBy(0f); mViewPager.endFakeDrag(); } };
This is the solution to my problem of applying conversion. However, there is a flash of an empty view holder visible before the transformation is applied. I am on my way. I am new to Android and have no idea how to solve any of the problems. If problem 1 itself is resolved, I don’t have to look for workarounds.
Below is the code to add a view and inflate the previous view with an empty layout
public int addView(View v, int position) { prevPos = currPos; currPos = position; LayoutInflater inflater = (LayoutInflater) mContext .getSystemService(Context.LAYOUT_INFLATER_SERVICE); if(prevPos != -1) { views.set(prevPos, ((View) inflater.inflate(R.layout.empty_template, null, false))); } views.set(currPos, v); return position; }
Logcat:
06-20 16:50:54.890: D/AndroidRuntime(8810): Shutting down VM 06-20 16:50:54.890: W/dalvikvm(8810): threadid=1: thread exiting with uncaught exception (group=0x40bc51f8) 06-20 16:50:54.914: E/AndroidRuntime(8810): FATAL EXCEPTION: main 06-20 16:50:54.914: E/AndroidRuntime(8810): java.lang.NullPointerException 06-20 16:50:54.914: E/AndroidRuntime(8810): at android.support.v4.view.ViewPager.endFakeDrag(ViewPager.java:2234) 06-20 16:50:54.914: E/AndroidRuntime(8810): at mpkgname.mactivityname$1.run(mactivityname.java:202) 06-20 16:50:54.914: E/AndroidRuntime(8810): at android.os.Handler.handleCallback(Handler.java:605) 06-20 16:50:54.914: E/AndroidRuntime(8810): at android.os.Handler.dispatchMessage(Handler.java:92) 06-20 16:50:54.914: E/AndroidRuntime(8810): at android.os.Looper.loop(Looper.java:137) 06-20 16:50:54.914: E/AndroidRuntime(8810): at android.app.ActivityThread.main(ActivityThread.java:4514) 06-20 16:50:54.914: E/AndroidRuntime(8810): at java.lang.reflect.Method.invokeNative(Native Method) 06-20 16:50:54.914: E/AndroidRuntime(8810): at java.lang.reflect.Method.invoke(Method.java:511) 06-20 16:50:54.914: E/AndroidRuntime(8810): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790) 06-20 16:50:54.914: E/AndroidRuntime(8810): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557) 06-20 16:50:54.914: E/AndroidRuntime(8810): at dalvik.system.NativeStart.main(Native Method) 06-20 16:51:19.992: D/dalvikvm(8810): Debugger has detached; object registry had 658 entries