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.

4

4 回答 4

1

我刚刚尝试使用 v13 支持库,我没有发现任何问题:

mPager.setPageTransformer(false, new PageTransformer() {

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

            }
        });

实际上ViewPagerv4 jar 中的 jar 与 v13 jar 中的完全相同。此外,PageTransformer根本不依赖于Fragments。你能提供一些代码吗?

编辑:

您缺少导入:

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

你的课上有吗?请确保您的匿名PageTransformer实际实现android.support.v4.view.ViewPager.PageTransformer而不是其他东西。

于 2013-11-14T00:07:59.223 回答
1

我在编写“使用 ViewPager 进行屏幕幻灯片”教程时遇到了同样的问题,但是打包的 v4 支持库版本不包含 PagerTransformer 接口。因此,删除 jar,并将其替换为 Android->extras->support->v13 文件夹中的版本。

于 2014-12-15T19:44:49.517 回答
1

添加compile 'com.android.support:support-v13:23.1.1'到文件dependenciesbuild.gradle

在此处输入图像描述

于 2016-04-20T19:57:06.227 回答
0

我希望这可以进一步帮助您,(我认为未找到编译器的符号是以下方法):

当目标平台低于 HoneyComb 时,您希望使用这些平台中不可用的 View 的这些方法(在 PageTransformer 实现的类中):

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

而是使用:

ViewCompat.setAlpha(view,1);
ViewCompat.setTranslationX(view,0);
ViewCompat.setScaleX(view,1);
ViewCompat.setScaleY(view,1);
于 2014-07-28T06:36:38.280 回答