11

I am using ViewPager for sliding effect in my app.

I have pager.setPageMargin() to make next and previous views visibility, unfortunately my next view is over lapping with current view.

Below is my code

pager = (ViewPager) findViewById(R.id.myviewpager);

    adapter = new MyPagerAdapter(this, this.getSupportFragmentManager());
    pager.setAdapter(adapter);
    pager.setOnPageChangeListener(adapter);



    // Set current item to the middle page so we can fling to both
    // directions left and right
    pager.setCurrentItem(FIRST_PAGE);

    // Necessary or the pager will only have one extra page to show
    // make this at least however many pages you can see
    pager.setOffscreenPageLimit(10);

    // Set margin for pages as a negative number, so a part of next and 
    // previous pages will be showed

    //
    pager.setPageMargin(-450);

I am using fragment in the pager. I am getting output as attached with this thread, but need views being place properly with out over lapping

enter image description here

part marked in above picture should go behind the centre card.

Suresh
  • 1,199
  • 2
  • 12
  • 36
  • possible duplicate of [Android ViewPager - Show preview of page on left and right](http://stackoverflow.com/questions/10098040/android-viewpager-show-preview-of-page-on-left-and-right) – Billy Riantono Sep 16 '15 at 10:55
  • 1
    @BillyRiantono its not duplicate, in the attached picture if you see i am displaying next card, but next card (marked in oval) is overlaid on current card(centre card) – Suresh Sep 16 '15 at 11:03
  • Hi @Suresh Did you have solution for your case ? – mdtuyen Oct 11 '16 at 07:06
  • Hi @Suresh Did you have solution for your problem – androi useru Jan 01 '22 at 07:29
  • @androiuseru sorry guy i dont recollect what is the solution I used to solve this since its long time. – Suresh Mar 21 '22 at 12:46

6 Answers6

1

since you are setting pager.setPageMargin(-450); you also need to give 240 padding to the ViewPage's child fragment and pager.setClipToPadding(false); for smooth animation.

if someone still looking for solution, I had customized the ViewPage to achieve it without using negative margin, find a sample project here https://github.com/44kksharma/Android-ViewPager-Carousel-UI it should work in most cases but you can still define page margin with mPager.setPageMargin(margin in pixel);

44kksharma
  • 2,740
  • 27
  • 32
0

Try this:

    pager.setClipToPadding(false);
    pager.setPadding(50, 0, 50, 0);

instead of:

    pager.setPageMargin(-450);
Mian Azhar
  • 57
  • 2
  • 10
0

Actually i brought the required view to the front

view.bringToFront();

Note: Want to identify the correct Layout which is in centre of view pager's current displaying page

Suresh
  • 1,199
  • 2
  • 12
  • 36
0
pager.setPadding(0, 0, 0, 0);
pager.setPageMargin(0, 0, 0, 0);

You just use match_parent for height and width of viewpaper tks. hope it useful for you

MrTy
  • 140
  • 1
  • 9
0

I’ve find a solution for post-KitKat version. Basically what I did is increase the elevation of the current center view. (I'm still working for pre-Lollipop solution)

Here's my code, Set an OnPageChangeListener to your viewpager and, in OnPageSelected Method

     @Override public void onPageSelected(int position) {

      //Counter for loop
      int count = 0;
      int PAGER_LOOP_THRESHOLD = 2;
      if (position >= PAGER_LOOP_THRESHOLD) {
        count = position - PAGER_LOOP_THRESHOLD;
      }
      do {
        Fragment fragment = (Fragment) adapter.instantiateItem(pager, count);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP
            && fragment.getView() != null) {

          //If it's current center position, increase view's elevation. Otherwise, we reduce elevation
          if (count == position) {
            fragment.getView().setElevation(8.0f);
          }
          else {
            fragment.getView().setElevation(0.0f);
          }
        }
        count++;
      } while (count < position + PAGER_LOOP_THRESHOLD);
    }
Vincent Paing
  • 2,308
  • 3
  • 17
  • 29
0

First, the viewpager items are stacked like a stack. so when it gives a margin value, only a space is added in between. For this, you should use transformer and set the translationZ value for these features.

There are usually 3 views and you have to set the position for each.

position < 0 -> {
  alpha = 1f
  translationX = -width * MAX_SCALE * position
  ViewCompat.setTranslationZ(this, position)
  val scaleFactor = (MIN_SCALE
                       + (1 - MIN_SCALE) * (1 - abs(position)))
  scaleX = scaleFactor
  scaleY = scaleFactor
}

Also you can define 0.75f max and min scale. For all code:

override fun transformPage(view: View, position: Float) {
        with(view) {
            when {
                position < -1 -> {
                    alpha = 1f
                }
                position < 0 -> {
                    alpha = 1f
                    translationX = -width * MAX_SCALE * position
                    ViewCompat.setTranslationZ(this, position)
                    val scaleFactor = (MIN_SCALE
                            + (1 - MIN_SCALE) * (1 - abs(position)))
                    scaleX = scaleFactor
                    scaleY = scaleFactor
                }
                position == 0f -> {
                    alpha = 1f
                    scaleX = 1f
                    scaleY = 1f
                    translationX = 0f
                    ViewCompat.setTranslationZ(this, 0f)
                }
                position <= 1 -> {
                    alpha = 1f
                    translationX = width * MAX_SCALE * -position
                    ViewCompat.setTranslationZ(this, -position)
                    val scaleFactor = (MIN_SCALE
                            + (1 - MIN_SCALE) * (1 - abs(position)))
                    scaleX = scaleFactor
                    scaleY = scaleFactor
                }
                else -> {
                    alpha = 1f
                }
            }
        }
    }

You should apply it to the class you created

class OverlapPageTransformer : ViewPager2.PageTransformer {
     override fun transformPage(view: View, position: Float) {}
}

Last;

 yourPager.apply {
     setPageTransformer(OverlapPageTransformer())
 }
ihaydinn
  • 199
  • 4
  • 16