| /* |
| * Copyright (C) 2014 The Android Open Source Project |
| * |
| * Licensed under the Apache License, Version 2.0 (the "License"); |
| * you may not use this file except in compliance with the License. |
| * You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| |
| package com.android.deskclock; |
| |
| import android.content.Context; |
| import androidx.viewpager.widget.ViewPager; |
| import android.util.AttributeSet; |
| import android.view.MotionEvent; |
| import android.view.View; |
| |
| public class VerticalViewPager extends ViewPager { |
| |
| public VerticalViewPager(Context context) { |
| this(context, null); |
| } |
| |
| public VerticalViewPager(Context context, AttributeSet attrs) { |
| super(context, attrs); |
| init(); |
| } |
| |
| /** |
| * @return {@code false} since a vertical view pager can never be scrolled horizontally |
| */ |
| @Override |
| public boolean canScrollHorizontally(int direction) { |
| return false; |
| } |
| |
| /** |
| * @return {@code true} iff a normal view pager would support horizontal scrolling at this time |
| */ |
| @Override |
| public boolean canScrollVertically(int direction) { |
| return super.canScrollHorizontally(direction); |
| } |
| |
| private void init() { |
| // Make page transit vertical |
| setPageTransformer(true, new VerticalPageTransformer()); |
| // Get rid of the overscroll drawing that happens on the left and right (the ripple) |
| setOverScrollMode(View.OVER_SCROLL_NEVER); |
| } |
| |
| @Override |
| public boolean onInterceptTouchEvent(MotionEvent ev) { |
| final boolean toIntercept = super.onInterceptTouchEvent(flipXY(ev)); |
| // Return MotionEvent to normal |
| flipXY(ev); |
| return toIntercept; |
| } |
| |
| @Override |
| public boolean onTouchEvent(MotionEvent ev) { |
| final boolean toHandle = super.onTouchEvent(flipXY(ev)); |
| // Return MotionEvent to normal |
| flipXY(ev); |
| return toHandle; |
| } |
| |
| private MotionEvent flipXY(MotionEvent ev) { |
| final float width = getWidth(); |
| final float height = getHeight(); |
| |
| final float x = (ev.getY() / height) * width; |
| final float y = (ev.getX() / width) * height; |
| |
| ev.setLocation(x, y); |
| |
| return ev; |
| } |
| |
| private static final class VerticalPageTransformer implements ViewPager.PageTransformer { |
| @Override |
| public void transformPage(View view, float position) { |
| final int pageWidth = view.getWidth(); |
| final int pageHeight = view.getHeight(); |
| if (position < -1) { |
| // This page is way off-screen to the left. |
| view.setAlpha(0); |
| } else if (position <= 1) { |
| view.setAlpha(1); |
| // Counteract the default slide transition |
| view.setTranslationX(pageWidth * -position); |
| // set Y position to swipe in from top |
| float yPosition = position * pageHeight; |
| view.setTranslationY(yPosition); |
| } else { |
| // This page is way off-screen to the right. |
| view.setAlpha(0); |
| } |
| } |
| } |
| } |