castorflex / VerticalViewPager

Copy pasted version of the support ViewPager, but vertical!
1.11k stars 307 forks source link

Using setCurrentItem() brings no visual effect #8

Closed reod closed 10 years ago

reod commented 10 years ago

As in title, besides that everything works perfect. On (FragmentPagerAdapter) myAdapter.getItem() I'm returning custom class which returns simple View in onCreateView().

Plus when I enable debug mode (DEBUG == true) I get: 06-02 09:35:44.470: E/AndroidRuntime(8694): FATAL EXCEPTION: main 06-02 09:35:44.470: E/AndroidRuntime(8694): java.lang.ClassCastException: com.example.mypackage.SubPage cannot be cast to android.view.View 06-02 09:35:44.470: E/AndroidRuntime(8694): at com.example.mypackage.VerticalViewPager.populate(VerticalViewPager.java:938) 06-02 09:35:44.470: E/AndroidRuntime(8694): at com.example.mypackage.VerticalViewPager.populate(VerticalViewPager.java:813) 06-02 09:35:44.470: E/AndroidRuntime(8694): at com.example.mypackage.VerticalViewPager$3.run(VerticalViewPager.java:216) 06-02 09:35:44.470: E/AndroidRuntime(8694): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749) 06-02 09:35:44.470: E/AndroidRuntime(8694): at android.view.Choreographer.doCallbacks(Choreographer.java:562) 06-02 09:35:44.470: E/AndroidRuntime(8694): at android.view.Choreographer.doFrame(Choreographer.java:531) 06-02 09:35:44.470: E/AndroidRuntime(8694): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735) 06-02 09:35:44.470: E/AndroidRuntime(8694): at android.os.Handler.handleCallback(Handler.java:725) 06-02 09:35:44.470: E/AndroidRuntime(8694): at android.os.Handler.dispatchMessage(Handler.java:92) 06-02 09:35:44.470: E/AndroidRuntime(8694): at android.os.Looper.loop(Looper.java:137) 06-02 09:35:44.470: E/AndroidRuntime(8694): at android.app.ActivityThread.main(ActivityThread.java:5074) 06-02 09:35:44.470: E/AndroidRuntime(8694): at java.lang.reflect.Method.invokeNative(Native Method) 06-02 09:35:44.470: E/AndroidRuntime(8694): at java.lang.reflect.Method.invoke(Method.java:511) 06-02 09:35:44.470: E/AndroidRuntime(8694): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 06-02 09:35:44.470: E/AndroidRuntime(8694): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 06-02 09:35:44.470: E/AndroidRuntime(8694): at dalvik.system.NativeStart.main(Native Method)

castorflex commented 10 years ago

what is your SubPage class?

reod commented 10 years ago

It's a Fragment, onCreateView() returns FrameLayout.

castorflex commented 10 years ago

Can you replace the VerticalViewPager with the ViewPager, present in the support library, to see if you still have your exception?

reod commented 10 years ago

Hmm. In this scenario setCurrentItem() doesn't work too and I get the same ClassCastException, from here:

if (DEBUG) {
Log.i(TAG, "populate() - destroyItem() with pos: " + pos +
" view: " + ((View) ii.object));
} 

But during normal swiping by hand, everything is OK. One more information: when I DEBUG is true, after setCurrentItem() i see, that nr of item in

if (DEBUG) {
            Log.i(TAG, "Current page list:");
            for (int i = 0; i < mItems.size(); i++) {
                Log.i(TAG, "#" + i + ": page " + mItems.get(i).position);
            }
        }

is fine. Only visible part won't update.

castorflex commented 10 years ago

Here is the implementation of setCurrentItem:

public void setCurrentItem(int item) {
    mPopulatePending = false;
    setCurrentItemInternal(item, !mFirstLayout, false);
}

The second parameter of setCurrentItemInternal defines if the scroll is animated. We see that it won't be animated until the viewpager is shown (actually, until onLayout is called). This is the wanted behavior.