0

我正在構建一個類似於inshorts的android應用程序。我想上下滑動我的cardView。我在做什麼 - 我使用webAPI解析服務器中的數據,然後使用cardView中的BaseAdapter顯示。但現在我需要向上滑動它應該向上移動並顯示下一張位置卡。你可以這樣做如何讓cardView在android中上下滑動動畫?

回答

0

一種方式是通過使用fragmentsViewPager加載頁面,並且可以指定屬性動畫動畫片段。

如果你是新來的片段,嘗試從here

開始然後你需要ViewPager頁面之間刷卡。 here

屬性動畫通常用於爲原生片段(Android.App.Fragment)設置動畫。 see this 看到這swap fragments using Animation

1

您可以ViewPager片段做到這一點,那麼你需要把幻燈片動畫與視圖尋呼機。對於橫向滑動,您可以看到開發人員參考http://developer.android.com/training/animation/screen-slide.html

之後,你需要修改視圖尋呼機略,使其行爲類似於垂直而非水平,你可以用下面的代碼實現這一點:

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 ViewPager.PageTransformer { 
private static final float MIN_SCALE = 0.75f; 
    @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 <= 0) { // [-1,0] 
      // Use the default slide transition when moving to the left page 
      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); 
      view.setScaleX(1); 
      view.setScaleY(1); 

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

      // Counteract the default slide transition 
      view.setTranslationX(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); 
     } 

    } 

} 

/** 
* 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)); 
} 
}