具有无限viewpager的PageTransformer

时间:2016-11-10 07:28:01

标签: android android-viewpager android-pagetransformer

我已将TabletTransformer(使用here的源代码)与无限视图寻呼机循环结合起来。

结果不是我们的预期。它没有编译错误,但在页面之间滑动时可能会出现空白页。

enter image description here

主要活动

public class MainActivity extends AppCompatActivity
{
    public ViewPager mPager;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        // Initialize ViewPager
        mPager = (ViewPager) findViewById(R.id.container);

        InfiniteViewPagerAdapter adapter = new InfiniteViewPagerAdapter(getSupportFragmentManager());
        List<Fragment> pagerFragments = createPagerFragments();
        adapter.setPagerFragments(pagerFragments);
        mPager.setAdapter(adapter);

        mPager.setPageTransformer(true, new TabletTransformer());
        mPager.setCurrentItem(1);
    } 

    private List<Fragment> createPagerFragments() 
    {
        List<Fragment> dataList = new LinkedList<Fragment>();

        dataList.add(0, FirstFragment.newInstance("First page"));
        dataList.add(1, SecondFragment.newInstance("Main page"));
        dataList.add(2, ThirdFragment.newInstance("Third page"));
        dataList.add(3, FourthFragment.newInstance("Fourth page"));

        return dataList;
    }
}

activity_main.xml中

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.example.wlwong.tablettransformer.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

    <com.example.wlwong.tablettransformer.InfiniteViewPager.InfiniteViewPager
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</android.support.design.widget.CoordinatorLayout>

我调用了2个类来实现Infinite View-Pager循环: InfiniteViewPager InfiniteViewPagerAdapter

InfiniteViewPager类

    public class InfiniteViewPager extends ViewPager {

    private OnPageChangeListener externalOnPageChangeListener = null;

    public InfiniteViewPager(Context context) {
        super(context);
        setActualOnPageChangeListener(new PageChangeListener(this));
    }

    public InfiniteViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
        setActualOnPageChangeListener(new PageChangeListener(this));
    }

    private void setActualOnPageChangeListener(OnPageChangeListener listener) {
        super.setOnPageChangeListener(listener);
    }

    @Override
    public void setOnPageChangeListener(OnPageChangeListener listener) {
        this.externalOnPageChangeListener = listener;
    }

    private OnPageChangeListener getExternalOnPageChangeListener() {
        return this.externalOnPageChangeListener;
    }

    private static class PageChangeListener implements OnPageChangeListener {

        private final InfiniteViewPager viewPager;

        public PageChangeListener(final InfiniteViewPager viewPager) {
        this.viewPager = viewPager;
        }

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            final OnPageChangeListener externalOnPageChangeListener =
                viewPager.getExternalOnPageChangeListener();
            if (externalOnPageChangeListener != null) {
            externalOnPageChangeListener.onPageScrolled(
                    position, positionOffset, positionOffsetPixels);
            }
        }    

        @Override
        public void onPageSelected(int position) {
            final OnPageChangeListener externalOnPageChangeListener =
                viewPager.getExternalOnPageChangeListener();
            if (externalOnPageChangeListener != null) {
                externalOnPageChangeListener.onPageSelected(position);
            }

            InfiniteViewPagerAdapter adapter = (InfiniteViewPagerAdapter) viewPager.getAdapter();
            List<Fragment> pagerFragments = adapter.getPagerFragments();
            // Ensure that cycling only occurs if there are 3 or more fragments.
            if (pagerFragments.size() > 2) {
                final int cycleResult = cyclePagerFragments(pagerFragments, position);
                if (cycleResult != 0) {
                    adapter.setPagerFragments(pagerFragments);
                    adapter.notifyDataSetChanged();

                    // Turn off the actual and external OnPageChangeListeners, so that
                    // this function does not unnecessarily get called again when
                    // setting the current item.
                    viewPager.setOnPageChangeListener(null);
                    viewPager.setActualOnPageChangeListener(null);
                    viewPager.setCurrentItem(position + cycleResult, false);
                    viewPager.setOnPageChangeListener(externalOnPageChangeListener);
                    viewPager.setActualOnPageChangeListener(this);
                }
            }
        }

        @Override
        public void onPageScrollStateChanged(int state) {
            final OnPageChangeListener externalOnPageChangeListener =
                viewPager.getExternalOnPageChangeListener();
            if (externalOnPageChangeListener != null) {
                externalOnPageChangeListener.onPageScrollStateChanged(state);
            }
        }
    }

    private static int cyclePagerFragments(List<Fragment> pagerFragments, final int position) {
        final int lastPosition = pagerFragments.size() - 1;
        if (position == lastPosition) {
            pagerFragments.add(pagerFragments.remove(0));
            return -1;
        } else if (position == 0) {
            pagerFragments.add(0, pagerFragments.remove(lastPosition));
            return 1;
        }

        return 0;
    }
}

InfiniteViewPagerAdapter类

    public class InfiniteViewPagerAdapter extends FragmentStatePagerAdapter {

    private List<Fragment> pagerFragments;

    public InfiniteViewPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    public List<Fragment> getPagerFragments() {
        return pagerFragments;
    }

    public void setPagerFragments(final List<Fragment> pagerFragments) {
        this.pagerFragments = pagerFragments;
    }

    @Override
    public Fragment getItem(int position) {
        return (pagerFragments != null && pagerFragments.size() > position) ?
            pagerFragments.get(position) : null;
    }

    @Override
    public int getCount() {
        return (pagerFragments != null) ? pagerFragments.size() : 0;
    }

    @Override
    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }
}    

我已经尝试但未能找到导致的问题。或者实际上合并TabletTransformerInfinite view-pager是不可行的?

0 个答案:

没有答案