Android在ViewPager中设置动画效果

时间:2014-08-11 15:51:55

标签: android android-viewpager

嗨我有一个加载片段的视图寻呼机,我想在ImageView中添加一些动画,使其在ViewPager滚动时向上移动。我遇到的问题是viewPager为前两页创建了视图,因此动画不会在这些页面上发生。是否有办法检测viewpager是否正在滚动或页面是否已更改?

继续我到目前为止所尝试的内容 - 继承我的viewPagerAdapter

 @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {



            if(page == 0){
                view = inflater.inflate(R.layout.layout_tutorial, container, false);    
            }else if(page == 1){
                view = inflater.inflate(R.layout.layout_tutorial, container, false);


                tutImage = (ImageView)view.findViewById(R.id.tutImage);
                int imageResource = getResources().getIdentifier("tut_view_2", "drawable", context.getPackageName());
                Drawable tutImageDrawable = getResources().getDrawable(imageResource);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                    tutImage.setBackground(tutImageDrawable);
                }else{
                    tutImage.setBackgroundDrawable(tutImageDrawable);
                }

            }else if(page == 2){
                view = inflater.inflate(R.layout.layout_tutorial, container, false);



                tutImage = (ImageView)view.findViewById(R.id.tutImage);
                int imageResource = getResources().getIdentifier("tut_view_3", "drawable", context.getPackageName());
                Drawable tutImageDrawable = getResources().getDrawable(imageResource);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                    tutImage.setBackground(tutImageDrawable);
                }else{
                    tutImage.setBackgroundDrawable(tutImageDrawable);
                }

继承人我的观察者

 public static class MyPagerAdapter extends FragmentPagerAdapter {

            private Context context;

            public MyPagerAdapter(android.support.v4.app.FragmentManager fragmentManager,Context c) {

                super(fragmentManager);
                context = c;
            }


            @Override
            public int getCount() {
                int levelCount = 4;


                return levelCount;
            }

            // Returns the fragment to display for that page
            @Override
            public TutorialAdapter getItem(int position) {
                String data = null;

                return TutorialAdapter.newInstance(position, data, context);

            }


            @Override
            public CharSequence getPageTitle(int position) {
                return "Page " + position;
            }

        }

1 个答案:

答案 0 :(得分:0)

您需要使用OnPageChangeListener,将其设置为您的viewpager:

    viewPager.setOnPageChangeListener(myOnPageChangeListener);

OnPageChangeListener实现了一些方法,如:

    @Override
    public void onPageScrollStateChanged(int state) {}

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}

    @Override
    public void onPageSelected(int position) {}

他们可以帮助你。请查看此信息以获取更多信息:ViewPager.OnPageChangeListener