0

我創建了一個垂直ViewPager,使用ViewPage.PageTransformer並交換了X和Y座標。 (我使用this approach垂直ViewPager。停止滾動過去某個點

現在,我想要做的是停止滾動在某個點(我想最後一個視圖只佔屏幕高度的65%,但全寬)。 通常,在這種情況下,我會覆蓋getPageWidth(),但由於我的寬度和高度現在有點混雜,所以當我這樣做時,我的視圖佔用了屏幕高度和寬度的65%。

那麼我該如何解決這個問題?

謝謝!

MyViewPager.java

public class MyViewPager extends ViewPager { 

    public MyViewPager(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     init(); 
    } 

    private void init() { 
     setPageTransformer(true, new VerticalPageTransformer()); 
     setOverScrollMode(OVER_SCROLL_NEVER); 
    } 

    private class VerticalPageTransformer implements ViewPager.PageTransformer { 

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

      if (position < -1) { 
       view.setAlpha(0); 

      } else if (position <= 1) { 
       view.setAlpha(1); 

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

       float yPosition = position * view.getHeight(); 
       view.setTranslationY(yPosition); 

      } else { 
       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 onTouchEvent(MotionEvent ev) { 
     return super.onTouchEvent(swapXY(ev)); 
    } 

    @Override 
    public boolean onInterceptTouchEvent(MotionEvent ev){ 
     boolean intercepted = super.onInterceptTouchEvent(swapXY(ev)); 
     swapXY(ev); 
     return intercepted; 
    } 
} 

MyPagerAdapter.java

public class MyPagerAdapter extends PagerAdapter { 

    private Context context; 
    private LayoutInflater layoutInflater; 

    public MyPagerAdapter(Context context) { 
     this.context = context; 
     layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    } 

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

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

    @Override 
    public Object instantiateItem(ViewGroup container, final int position) { 

     if (position == 0) { 
      View view = layoutInflater.inflate(R.layout.item_profile_picture, container, false); 

      container.addView(view); 
      return view; 
     } 

     else { 
      View view = layoutInflater.inflate(R.layout.item_profile_info, container, false); 

      container.addView(view); 
      return view; 
     } 

    } 

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

    @Override 
    public float getPageWidth(int position) { 
     if (position == 1) return (0.65f); 
     return super.getPageWidth(position); 
    } 

} 

回答

0

嗨,據我瞭解你們停在某一點,而無需在反彈時。將overscroll(從不)方法添加到所需的viewpager。並管理對象的邊距。儘可能地包裝內容。

+0

你能提供我如何做到這一點的例子嗎? –

+0

setOverScrollMode(OVER_SCROLL_NEVER);添加這一個,並管理您的項目在XML我不在PC – Izvorski

+0

我從來沒有overscroll,但更改xml不起作用,因爲重寫'getPageWidth'改變視圖的寬度和高度@Izvorski –