6

swipe up and down effect like those news apps inshorts,hike news,murmur.

whole layout smoothly up/down.
check app on this link inshorts and murmur.

i tried this code...

    public class VerticalViewPager extends ViewPager {
    public VerticalViewPager(Context context) {
        super(context);
        init();
    }

    public VerticalViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }
    private void init() {
        // The majority of the magic happens here
        setPageTransformer(true, new VerticalPageTransformer());
        // The easiest way to get rid of the overscroll drawing that happens on the left and right
        setOverScrollMode(OVER_SCROLL_NEVER);
    }
    private class VerticalPageTransformer implements PageTransformer {

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

            if (position < -1) { // [-Infinity,-1)
                // This page is way off-screen to the left.
                view.setAlpha(0);

            } else if (position <= 1) { // [-1,1]
                view.setAlpha(1);

                // Counteract the default slide transition
                view.setTranslationX(view.getWidth() * -position);

                //set Y position to swipe in from top
                float yPosition = position * view.getHeight();
                view.setTranslationY(yPosition);

            } else { // (1,+Infinity]
                // This page is way off-screen to the right.
                view.setAlpha(0);
            }
        }
    }

    /**
     * Swaps the X and Y coordinates of your touch event.
     */
    private MotionEvent swapXY(MotionEvent ev) {
        float width = getWidth();
        float height = getHeight();

        float newX = (ev.getY() / height) * width;
        float newY = (ev.getX() / width) * height;

        ev.setLocation(newX, newY);

        return ev;
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev){
        boolean intercepted = super.onInterceptTouchEvent(swapXY(ev));
        swapXY(ev); // return touch coordinates to original reference frame for any child views
        return intercepted;
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        return super.onTouchEvent(swapXY(ev));
    }

}

In MainActivity.java

VerticalViewPager Pager2;
PagerAdapter adapter;
String[] articleTitle;
String[] articleName;
String[] articleDiscription;

OnCreate()

Pager2=(VerticalViewPager)findViewById(R.id.pager);


// Pass results to ViewPagerAdapter Class
adapter = new ViewPagerAdapter(getActivity(), articleTitle, articleName,     articleDiscription, btnBack,articleImage);

// Binds the Adapter to the ViewPager
Pager2.setAdapter(adapter);

activity_main.xml

<com.example.flipnews.VerticalViewPager
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/pager"
   />

In my code simple up-down swipe done,like this link. but i want to create better animation effect like above mentioned apps. or phone inbuilt photo gallery effect.

Thanks in advance.

Community
  • 1
  • 1
Viveka Patel
  • 666
  • 7
  • 17
  • Hello, Viveka... we can't help you to develop things... we only can help you to issues with that implementation. You need to try it by your own and post your code with the issues later. – Mariano Zorrilla Dec 04 '15 at 12:30
  • 1
    @Viveka if you found any lib for this animation , do let me know. – Pankaj Arora Dec 07 '15 at 06:22
  • 1
    I already try this code but did't get proper output..kindly check my code. - @MarianoZorrilla – Viveka Patel Dec 07 '15 at 12:58
  • 1
    i edited my post.. i didn't use any lib for animation. my question is same for animation. how can i apply in my code??-@AndeloperDev – Viveka Patel Dec 07 '15 at 13:05
  • This issue has been answered in detail in this thread:- http://stackoverflow.com/questions/13477820/android-vertical-viewpager – Achillies Nov 23 '16 at 19:22

1 Answers1

13

I found solution after many research i hope its helpful for others.

Tip: set view pager background color black for better swipe effect.

 private static class VerticalPageTransformer implements PageTransformer {
        private static float MIN_SCALE = 0.75f;

        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(0);



            } else if (position <= 0) { // [-1,0]
                // Use the default slide transition when moving to the left page
                view.setAlpha(1);
                //view.setTranslationX(1);
                view.setScaleX(1);
                view.setScaleY(1);
                float yPosition = position * view.getHeight();
                view.setTranslationY(yPosition);
                view.setTranslationX(-1 * view.getWidth() * position);

            } else if (position <= 1) { // (0,1]
                // Fade the page out.
                view.setAlpha(1 - position);

                view.setTranslationX(-1 * view.getWidth() * position);

                // Scale the page down (between MIN_SCALE and 1)
               float scaleFactor = MIN_SCALE
                        + (1 - MIN_SCALE) * (1 - Math.abs(position));
                view.setScaleX(scaleFactor);
                view.setScaleY(scaleFactor);

            } else { // (1,+Infinity]
                // This page is way off-screen to the right.
                view.setAlpha(0);
            }

        }
    }

    private MotionEvent swapXY(MotionEvent ev) {
        float width = getWidth();
        float height = getHeight();

        float newX = (ev.getY() / height) * width;
        float newY = (ev.getX() / width) * height;

        ev.setLocation(newX, newY);

        return ev;
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev){
        boolean intercepted = super.onInterceptTouchEvent(swapXY(ev));
        swapXY(ev); // return touch coordinates to original reference frame for any child views
        return intercepted;
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        return super.onTouchEvent(swapXY(ev));
    }

}
Viveka Patel
  • 666
  • 7
  • 17
  • @Viveka I have a problem my view pager have a grid view inside it. when swiping on the grid swipe function is not working – Praneeth Feb 18 '16 at 05:00
  • Hi, i tried your answer and it works great. Im a beginner in this. Can you give me a short description on what exactly are you doing here, for example `view.setTranslationX(-1 * view.getWidth() * position);` this? – Vishnu Jul 17 '16 at 16:03
  • I mean, the purpose of the calculations. – Vishnu Jul 17 '16 at 17:20
  • @Viveka Have you also added right and left swap feature? – Faisal Shaikh Jan 09 '18 at 08:57