ViewPager2 without using Viewpager2 - android

I want to create my own version of ViewPager2 so that I can control the animation design and orientation capabilities. Basically, I'm trying to have a scrolling up and down load new users while left and right would be more information about that user.
I tried using custom animations with swipe gestures, which works exactly as I wanted, except I don't get the control of dynamically animating the swiping direction. Essentially, it still works like a button animation - you might swipe in the direction you want but the animation speed/feel/etc is always the same animation vs. ViewPager2's ability to move with the gesture.
I experimented with OnDragListeners which do give me the "Tinder card movement" animation effect but I don't want to allow for animations in all directions. I was able to use a ViewPager2 for the horizontal direction while using the OnDragListener to override the swiping (using OnLongClick). When you drag up or down it has the same issue of the transition animation being the same speed/feel/etc everytime instead of the nice ViewPager2 slide on touch effect.
I only need 4 fragments for each user so my next idea was to try a horizontalscrollview in a recyclerview but that's working either.
Any suggestions for an approach I might not have considered?

Related

Is it possible to customize the snap behavior of `ViewPager2`?

I need to emulate the behavior of Instagram's Reels which scrolls to the next or previous page when the current page is scrolled more than 50% upwards or downwards respectively and the finger goes up. ViewPager2's default behavior isn't satisfactory for this and I don't think I can change it without changing the PagerSnapHelper inside ViewPager2.
Is there any other way I can implement a custom Snap behavior for ViewPager2 then?

Implementing a Custom ViewPager

I'm trying to create a layout manager similar to ViewPager, but I'm not sure about what the best way to implement it is.
Basically I want my views to look like this:
From there I want to transition to other views when the user swipes from any of the numbered rectangles on the sides. So, say, if the user swipes from view number 3 and to the left, I would like to transition to the following view:
Ideally, I would like the transitions to be similar to how ViewPager works (i.e. I want the transition to interact with your finger so that the transition progresses back and forth when the user moves his finger from left to right).
It seems like I will need to implement my own version of ViewPager, I'm just not sure how to go about doing this. I only need to handle a small number of simple views so I can probably get away with caching all of my views for better performance.
I know how to implement the code to detect the swipes and where in the view the swipe started, but I don't know what the proper way to implement the view transitions is and how to handle the states of all the different views.
Any pointers are greatly appreciated!
Thanks in advance.

Custom swipe animation (Tilt, Card Deck, 3D, etc.)

How can I implement a custom swipe animation instead of the standard horizontal movement? I'm thinking about a mixture of ViewPager (swipe to scroll) and ViewFlipper (custom animation). It's important that the animation gives a live feedback to the user's finger movement, not just plays after a swipe has been completed.
Some examples:
http://www.youtube.com/watch?feature=player_embedded&v=BcloiEZa1iI
ICS launcher tilt effect
Card deck effect in ICS launcher menu
Cube effect of custom launchers
Any idea on how to generally override the swipe animation in a ViewPager?
You will have to override onTouchEvent(), onScroll() and the likes. Point is that the ViewPager's swipe is no animation but actually scrolls the views around. Animations wont work for you here is my bet.

Android Gallery Jumps when swiped quickly or slowly

I've made an activity that looks a lot like the level select screen from angry birds: there is a grid of button, and you can scroll through pages of them by swiping right or left.
I built it by creating a layout of the buttons, and then adding those layout to a Gallery view.
The problem is the animation is jerky, even if you swipe extra slowly, the content jumps ahead. Even when you fling the gallery page, it skips and jumps along its way to its destination.
I am wondering how to fix this: Maybe the complex layout it making it non responsive during the inflate?
Do you know how to fix this, or of a good way to do a workaround using some other approach that will let me have 3 or more screens smoothly swiping from page to page?
You should be using ViewPager. This is available in android support packages. Gallery Widget will not be smooth if you add components into it which has a lot of touch actions.
http://android-developers.blogspot.in/2011/08/horizontal-view-swiping-with-viewpager.html

Android: Next/Previous fling that sticks to the finger similar to Home Screen

I want to give my app a nice touch by allowing users to slide the page left or right instead of just using next/previous buttons (similar to the home screen).
What is the best way to do that? I assume I would have to override one of the Activity.on... methods and that I would also have to put my page's main View in a ViewGroup that allows me to shift pages left and right.
ViewFlipper is your friend!
Here you can see a nice video of the ViewFlipper in action and also a very good tutorial:
http://www.inter-fuser.com/2009/07/android-transistions-slide-in-and-slide.html
The solution is even easier these days with the release of Compatibility Package r3. You can download here: http://developer.android.com/sdk/compatibility-library.html
It includes
ViewPager: A ViewGroup that manages the layout for the child views, which the user can swipe between.
PagerAdapter: An adapter that populates the ViewPager with the views that represent each page.
and Fragment versions of those, if you are that way inclined.
The pager code is compatible back to API version 4 (1.6), and I just implemented a dynamically generated viewPager coming off a dynamically generated ListView in about 2 hours. I'm a novice, so this is definitely the preferred path.
There is an example app here: http://geekyouup.blogspot.com/2011/07/viewpager-example-from-paug.html
If one wants to flip between two activities perhaps one can apply this animated transition:
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
startActivity(intent);
finish();
//transition using XML view animations
overridePendingTransition(R.anim.slideinfromright, R.anim.slideouttoleft);
Use GestureDetector to detect if the touch event is a scroll.
If the first event to the first call to onScroll is ACTION_DOWN then you should see if it was a dominantly horizontal scroll. If so then your scroll is started and you should shift the absolute position of the view that fills the page.
For non deprecated absolute positioning, see my answer here Android: Alternative to AbsoluteLayout (I really do need absolute positioning)
You will want to be cautions of whether you return true to consume the touch events or not.
GestureDetector does not have a callback for scrolling having stopped. You will have to check if there was an ACTION_UP before you call GestureDetector.onTouchEvent and if there was an action up and you did have an unfinished scroll then you should set the absolute position to the destination location and use a TranslateAnimation to make it look nice moving from current to destination.
Edit:
GestureDetector did not work well at all if the child views also wanted to respond to touch events. I ended up creating a subclass of FrameLayout (one of the most basic layouts and the closest thing to a non intrusive parent view) and overriding dispatchTouchEvent. I just took all the events and did the detection myself.
cant we use Gallery view here?? with the adapter the whole page can be inflated inside gallery view adapter getView() and it will manage the left right scrolling perfectly.

Categories

Resources