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.