使用ViewPager实现像iOS应用程序一样closures垂直滑动即可closures的function

我目前有ViewPager水平排列的视图,并可以循环他们与PagerAdapter。 目前,为了执行我想要刷卡的操作,我必须在视图的页面上双击。 我可以发布代码,但是提取必要的作品有些困难…

我想要的是在这些视图上垂直滑动的能力,让它们通过轻扫和淡出垂直翻译,然后在距离设备边缘一定距离时执行一个动作。

要了解我在想什么,在“图库”应用程序中,您可以捏合打开的照片以缩小并打开水平幻灯片视图。 从那里你可以向上(或向下)的照片/video删除它。 对于那些没有相同的图库应用程序,这就像在iOS上closures应用程序。

我已经尝试扫描图库应用程序的源代码,但没有find正确的活动运气。

Related of "使用ViewPager实现像iOS应用程序一样closures垂直滑动即可closures的function"

view.setOnTouchListener(new View.OnTouchListener(){ public boolean onTouch(View v, MotionEvent motion) { float y = motion.getY(); /* NOTE: the following line might need to be in runOnUiThread() */ view.animate().alpha(1-Math.abs(y-height/2)/(height/2)).setDuration(50).start(); return true; //false if you want to pass this event on to other listeners } }); 

使用1-Math.abs(y-height/2)/(height/2)是,当我处于中心位置时,我希望alpha为1,而当alpha位于顶部或底部时为0 。 你必须确定自己如何获得height值,或者如果你想使用不同的方法来计算阿尔法。 如果你想获得相对于屏幕的触摸位置,而不是相对于视图的位置,使用getRawY()

此外,对于您来说,了解如果MotionEvent是按下,拖动或释放事件,分别对motion.getAction() ==motion.getAction() ==使用motion.getAction() ==可能会很有用。

我通过克隆编写良好的Android-SwipeToDismiss库,然后用ViewPagerreplaceListView代码,最终获得了这个工作。

成品看起来像这样。

最终结果

检查下面的代码,这可能对你有帮助:

  public class MainActivity extends Activity implements View.OnTouchListener{ private RelativeLayout baseLayout; private int previousFingerPosition = 0; private int baseLayoutPosition = 0; private int defaultViewHeight; private boolean isClosing = false; private boolean isScrollingUp = false; private boolean isScrollingDown = false; @Override protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_popup); baseLayout = (RelativeLayout) findViewById(R.id.base_popup_layout);//this is the main layout baseLayout.setOnTouchListener(this); } public boolean onTouch(View view, MotionEvent event) { // Get finger position on screen final int Y = (int) event.getRawY(); // Switch on motion event type switch (event.getAction() & MotionEvent.ACTION_MASK) { case MotionEvent.ACTION_DOWN: // save default base layout height defaultViewHeight = baseLayout.getHeight(); // Init finger and view position previousFingerPosition = Y; baseLayoutPosition = (int) baseLayout.getY(); break; case MotionEvent.ACTION_UP: // If user was doing a scroll up if(isScrollingUp){ // Reset baselayout position baseLayout.setY(0); // We are not in scrolling up mode anymore isScrollingUp = false; } // If user was doing a scroll down if(isScrollingDown){ // Reset baselayout position baseLayout.setY(0); // Reset base layout size baseLayout.getLayoutParams().height = defaultViewHeight; baseLayout.requestLayout(); // We are not in scrolling down mode anymore isScrollingDown = false; } break; case MotionEvent.ACTION_MOVE: if(!isClosing){ int currentYPosition = (int) baseLayout.getY(); // If we scroll up if(previousFingerPosition >Y){ // First time android rise an event for "up" move if(!isScrollingUp){ isScrollingUp = true; } // Has user scroll down before -> view is smaller than it's default size -> resize it instead of change it position if(baseLayout.getHeight()<defaultViewHeight){ baseLayout.getLayoutParams().height = baseLayout.getHeight() - (Y - previousFingerPosition); baseLayout.requestLayout(); } else { // Has user scroll enough to "auto close" popup ? if ((baseLayoutPosition - currentYPosition) > defaultViewHeight / 4) { closeUpAndDismissDialog(currentYPosition); return true; } // } baseLayout.setY(baseLayout.getY() + (Y - previousFingerPosition)); } // If we scroll down else{ // First time android rise an event for "down" move if(!isScrollingDown){ isScrollingDown = true; } // Has user scroll enough to "auto close" popup ? if (Math.abs(baseLayoutPosition - currentYPosition) > defaultViewHeight / 2) { closeDownAndDismissDialog(currentYPosition); return true; } // Change base layout size and position (must change position because view anchor is top left corner) baseLayout.setY(baseLayout.getY() + (Y - previousFingerPosition)); baseLayout.getLayoutParams().height = baseLayout.getHeight() - (Y - previousFingerPosition); baseLayout.requestLayout(); } // Update position previousFingerPosition = Y; } break; } return true; } } 

对于animation使用下面的方法:

  public void closeUpAndDismissDialog(int currentPosition){ isClosing = true; ObjectAnimator positionAnimator = ObjectAnimator.ofFloat(baseLayout, "y", currentPosition, -baseLayout.getHeight()); positionAnimator.setDuration(300); positionAnimator.addListener(new Animator.AnimatorListener() { . . . @Override public void onAnimationEnd(Animator animator) { finish(); } . . . }); positionAnimator.start(); } public void closeDownAndDismissDialog(int currentPosition){ isClosing = true; Display display = getWindowManager().getDefaultDisplay(); Point size = new Point(); display.getSize(size); int screenHeight = size.y; ObjectAnimator positionAnimator = ObjectAnimator.ofFloat(baseLayout, "y", currentPosition, screenHeight+baseLayout.getHeight()); positionAnimator.setDuration(300); positionAnimator.addListener(new Animator.AnimatorListener() { . . . @Override public void onAnimationEnd(Animator animator) { finish(); } . . . }); positionAnimator.start(); }