4

My application uses Android's ViewPager with a FragmentStatePagerAdapter and it only needs to support Android 4.x or above. I'm using the v13 support library for the ViewPager so I don't need to deal with SupportFragments, I just have to use Android.app.Fragment. Everything works as expected, but now I'm trying to add a PageTransformer to my ViewPager and I'm running into problems.

ViewPager.setPageTransformer(bool, PageTransformer) isn't recognized as a method, and PageTransformer isn't recognized as a class (I've tried many combinations of imports to no avail). Using the v4 support library, ViewPager.setPageTransformer is a method, but if I use support library v4, I have to use the SupportFragments, which I thought was unnecessary due to the Android versions I'm supporting.

QUESTION: Does support v13 library's ViewPager support PageTransformers? Are there any benefits to using native Fragments over SupportFragments, or should I just use support library v4?

EDIT: I'm adding my file's imports and relevant code that demonstrates my problem

import java.lang.reflect.Field;
import android.app.Fragment;
import android.app.FragmentManager;
import android.support.v13.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

Inside my class (it doesn't extend anything), I'm trying to set the page transformer in this method:

public View createView(LayoutInflater inflater, ViewGroup container) {
        // Inflate the layout containing a title and body text.
        ViewGroup rootView = (ViewGroup) inflater
                .inflate(R.layout.carousel_fragment, container, false);


        mContainer = (PagerContainer) rootView.findViewById(R.id.pager_container);
        //PagerContainer makes it easy to display more than one page from the ViewPager
        //on screen at once
        mPager = mContainer.getViewPager();


        mAdapter = new MyPagerAdapter(MainActivity.getActivity().getFragmentManager());
        mPager.setAdapter(mAdapter);

        mPager.setOffscreenPageLimit(2);
        mPager.setPageMargin(10);
        mPager.setClipChildren(false);
        mPager.setCurrentItem(mPager.getAdapter().getCount()/2, false);

        mPager.setPageTransformer(false, new PageTransformer(){
            @Override
            public void transformPage(View page, float position) {
                // do something that isn't default behavior
            }
        });

        Field mScroller;
        try {
            mScroller = ViewPager.class.getDeclaredField("mScroller");

            mScroller.setAccessible(true); 
            CustomScroller scroller = new CustomScroller(MainActivity.getContext());

            mScroller.set(mPager, scroller);
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } 

        return rootView;
    }

The specific error I'm getting is cannot find symbol: PageTransformer, with a similar error for the setPageTransformer method. My project has the android-support-v13.jar but not the v4.jar because I was getting DEX errors when I included them both.

I'm probably doing something stupid, but I still can't figure it out. I appreciate any help you can give.

peteross
  • 236
  • 3
  • 12

4 Answers4

1

I just tried this with v13 support library, and i see no problems:

mPager.setPageTransformer(false, new PageTransformer() {

            @Override
            public void transformPage(View page, float position) {
                // TODO Auto-generated method stub

            }
        });

Actually the ViewPager in the v4 jar is exactly the same as in the v13 jar. Also, PageTransformer does not depends on Fragments at all. Could you provide some code?

EDIT:

You are missing an import:

import android.support.v4.view.ViewPager.PageTransformer;

Do you have it in your class? Please be sure that your anonymous PageTransformer actually implements android.support.v4.view.ViewPager.PageTransformer and not something else.

WonderCsabo
  • 11,947
  • 13
  • 63
  • 105
  • Thank you for your response. I've posted code in an edit to my original question to hopefully make my problem more clear. I'm trying exactly what you've posed with mPager.setPageTransformer, but I'm still getting errors. I know that PageTransformer doesn't depend on fragments, I see now that my original question was somewhat unclear and I hope my code snippet will help. – peteross Nov 14 '13 at 14:47
  • I tried importing the PageTransformer and I'm getting the same "cannot find symbol" error for PageTransformer. I'm using Android Studio, could my problem be caused by a bug there? I tried restarting the IDE and re-downloading the latest compatibility libraries but those didn't work either. – peteross Nov 14 '13 at 17:07
  • I just tried in Android Studio, and this works fine. Also, you have to use support v4 as well. Actually support v13 is just a superset of v4, it includes everything for v4. `ViewPager` is only included in the v4 package. Are you correctly importing the Support Library with Gradle? – WonderCsabo Nov 14 '13 at 18:14
1

I ran into the same issue when working on the "Using ViewPager for Screen Slides" tutorial, but the version of the v4 support library that is packaged does not contain the PagerTransformer interface. So delete the jar, and replace it with a version from your Android->extras->support->v13 folder.

jwBurnside
  • 849
  • 4
  • 31
  • 66
1

Add compile 'com.android.support:support-v13:23.1.1' to dependencies in build.gradle file:

enter image description here

AlessioX
  • 3,167
  • 6
  • 24
  • 40
Tony Baby
  • 7,176
  • 6
  • 18
  • 22
0

I hope this might help you further, (I think the symbols not compiler did not found are the methods as follows):

When targeting platform below HoneyComb, you would like to use these methods of a View (inside the PageTransformer implemented class) not available in these platforms:

View view=findViewById(R.id.viewInsidePager);
view.setAlpha(1);
view.setTranslationX(0);
view.setScaleX(1);
view.setScaleY(1);

Instead Use :

ViewCompat.setAlpha(view,1);
ViewCompat.setTranslationX(view,0);
ViewCompat.setScaleX(view,1);
ViewCompat.setScaleY(view,1);
Abhinav Saxena
  • 1,990
  • 2
  • 24
  • 55