如何修复添加了填充的VerticalViewPager?

时间:2017-05-28 18:56:04

标签: android android-layout android-viewpager

我有这个像魅力一样的VerticalViewPager:

import android.content.Context;
    import android.support.v4.view.ViewPager;
    import android.util.AttributeSet;
    import android.view.MotionEvent;
    import android.view.View;


    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 {

            @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 <= 1) { // [-1,1]
                    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);

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

    }

问题是当我添加填充时,例如:

android:paddingEnd 40dp               
android:paddingLeft 40dp                
android:paddingRight 40dp

android:paddingStart 40dp

视图进入屏幕的底角(基本上离开屏幕),我不知道如何修改代码中的transformPage来修复此请求帮助?

请求的MY布局文件:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <me.myapplication_v1.VerticalViewPager
            android:id="@+id/viewPager"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_below="@+id/imageView"
            android:layout_gravity="bottom"
            android:clipToPadding="false"
            android:overScrollMode="never"
            android:paddingBottom="30dp"
            android:paddingEnd="40dp"
            android:paddingLeft="40dp"
            android:paddingRight="40dp"
            android:paddingStart="40dp">

        </me.myapplication_v1.VerticalViewPager>

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_gravity="top|center"
            android:layout_marginBottom="13dp"
            android:layout_marginTop="14dp"
            android:cropToPadding="false"
            android:elevation="24dp"
            android:src="@drawable/semiosign" />

        <ImageButton
            android:id="@+id/back"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:background="#0000"
            android:paddingLeft="5dp"
            android:paddingTop="5dp"
            android:tint="@android:color/holo_red_light"
            app:srcCompat="@drawable/ic_action_arrow_back" />
    </RelativeLayout>

</FrameLayout>

有没有更好的verticalviewpager?

0 个答案:

没有答案