简体   繁体   中英

How to animate an item in the ViewPager after PagerTransform animation?

I am working with viewpager transformer. I am able add a transition effect using transformPage() method. The below given is my pager.

final ViewPager pager = (ViewPager) localView
            .findViewById(R.id.pager);
pager.setAdapter(new my_adapter());
pager.setPageTransformer(true, new PageTransformer() {

        @Override
        public void transformPage(View view, float position) {

            int pageWidth = view.getWidth();

            if (position < -1) { // [-Infinity,-1)
                // This page is way off-screen to the left.
                view.setAlpha(1);
            } else if (position <= 1) { // [-1,1]
                View dummyImageView = view.findViewById(R.id.tourImage);
                dummyImageView.setTranslationX(-position
                            * (pageWidth / 2)); // Half
                View imageBottom = view
                            .findViewById(R.id.tour_bottom_image);
                imageBottom.setTranslationX(-position
                            * (pageWidth / 10)); // Half speed
            } else { // (1,+Infinity]
                // This page is way off-screen to the right.
                view.setAlpha(1);

            }
        }
    });

And this is my adapter,

private class my_adapter extends PagerAdapter {

    @Override
    public int getCount() {
        return num_pages;
    }

    @Override
    public boolean isViewFromObject(View view, Object o) {
        return view == o;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View new_view = null;
        LayoutInflater inflater = getActivity().getLayoutInflater();
        new_view = inflater.inflate(R.layout.tour_page_one, null);
        TextView tvTourDesc = (TextView) new_view
                    .findViewById(R.id.tourDesc);
        ImageView imageTour = (ImageView) new_view
                    .findViewById(R.id.tourImage);
        ImageView imageBottomTour = (ImageView) new_view
                    .findViewById(R.id.tour_bottom_image);

        container.addView(new_view);
        return new_view;
    }
}

What I am trying to do is animate an item in the adapter, for instance imageTour imageView,once the transition animation is finished. When I add any animation it starts along with the pager transform animation. I want to animate it always after the transition animation. I tried using onPageChangeListener with the pager but it doesn't return any view other than the currentPage number so that i can add animation to imageTour(imageView).

In your transformPage() method:

if (position == 0.0) {  // page is settled in center
    // add animation to image
    // start the animation
}

I know an old question but somebody may need it, though.

    AnimateMyView(yourViewPager.getChildAt(position).findViewById(R.id.id_your_item_like_image_view));

    void AnimateMyView(View v){
        Animation anim = AnimationUtils.loadAnimation(this,
                R.anim.anim);
         v.startAnimation(anim);
     }

By the way I got some error on getChildAt(position) code and getChildAt(mViewPager.getChildCount()-1) fixed my problem. So you may need to change the code like that:

 AnimateMyView(yourViewPager.getChildAt(mViewPager.getChildCount()-1).findViewById(R.id.id_your_item_like_image_view));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM