Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Depth Pager Transformer just like SnapChat

I'm trying to implement ViewPager with DepthPageTransformer just like Snapchat application. In SnapChat application, there is a Camera screen which always comes in center of ViewPager and swiping from left or right brings other fragments on top of camera screen.

I've found code for DepthPageTransformer from this link. But the problem with this demo is that it brings all the next screen views from behind. Just like SnapChat I've a Camera screen in center and 2 screens coming on top from left and two screens coming on top from right on Camera screen.

So, how can I create a PageTransformer which brings fragments from left or right on the top of my center screen which is Camera?

like image 217
rahul shah Avatar asked Jun 12 '17 06:06

rahul shah


People also ask

What is the difference between ViewPager and ViewPager2?

ViewPager2 is an improved version of the ViewPager library that offers enhanced functionality and addresses common difficulties with using ViewPager . If your app already uses ViewPager , read this page to learn more about migrating to ViewPager2 .

Why use ViewPager?

ViewPager in Android allows the user to flip left and right through pages of data. In our android ViewPager application we'll implement a ViewPager that swipes through three views with different images and texts.

How does ViewPager work?

ViewPager - How It Works The idea is that ViewPager works with a PageAdapter to supply the Views that represent each page. The basic idea is that ViewPager keeps track of what page should be visible on the screen and then asks the PagerAdapter to get the View hierarchy that needs to be displayed.

How to make ViewPager in Android?

You can create swipe views using AndroidX's ViewPager widget. To use ViewPager and tabs, you need to add a dependency on ViewPager and on Material Components to your project. To insert child views that represent each page, you need to hook this layout to a PagerAdapter .


1 Answers

I think you should provide 5 fragments in your FragmentPagerAdapter, the third (index=2) fragment will be fragment with camera view,

viewPager.setCurrentItem(2); //2 is index of camera fragment

Then your ViewPager.PageTransformer should look like this:

@Override
public void transformPage(View page, float position) {
  int pageIndex = (int) page.getTag(); //im stroing pageIndex of fragment in its tag
  if(pageIndex == 2) { //2 is index of camera fragment
     float currentTranslation = - position * page.getWidth();
     ViewCompat.setTranslationX(page, currentTranslation);
     ViewCompat.setTranslationZ(page, -1);
     return;
}

I am stroing pageIndex while fragment's view is created in its tag.

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    ...
    view.setTag(pageIndex);
    return view;
}

Here is gif with results: https://media.giphy.com/media/OIwmXdr3nukq4/giphy.gif

Fragment with food is the one you should replace with your camera fragment.

like image 169
RadekJ Avatar answered Sep 24 '22 09:09

RadekJ