本文介绍了ViewPager PageTransformer与支持库V13的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序使用Android的ViewPager与FragmentStatePagerAdapter而且只需要支持Android 4.x或以上。我使用的ViewPager的V13支持库,所以我并不需要处理SupportFragments,我只需要使用 Android.app.Fragment 。一切都按预期工作,但现在我尝试添加 PageTransformer 来我ViewPager,我遇到了问题。

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(布尔,PageTransformer)不被识别为一个方法,而 PageTransformer 不认定为一类(我试过进口的多种组合无济于事)。使用V4支持库, ViewPager.setPageTransformer 是一种方法,但如果我使用支持库V4,我必须使用 SupportFragments ,我认为是不必要的,由于Android的版本,我支持。

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.

问:是否支持V13库的 ViewPager 支持PageTransformers?有没有使用原生片段在SupportFragments任何好处,或者我应该只使用支持库V4?

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?

编辑:我将我的文件的导入和相关的code演示我的问题

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;
    }

特定的错误我得到的找不到符号:PageTransformer ,与类似的错误了 setPageTransformer 方法。我的项目有 Android的支持 - v13.jar 但不是v4.jar当我列入他们两个,因为我得到DEX错误。

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.

我可能做一些愚蠢的,但我现在还不能看着办吧。我AP preciate任何帮助,您可以给。

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

推荐答案

我只是想这与V13支持库,我看不出有任何的问题:

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

            }
        });

其实, ViewPager 在V4罐子是完全一样的V13缸。此外, PageTransformer 不依赖于片段 s的一切。你能否提供一些code?

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?

编辑:

您缺少导入:

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

在你的类你有吗?请确保您的匿名 PageTransformer 真正实现 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.

这篇关于ViewPager PageTransformer与支持库V13的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-12 23:09