0

我有一个扩展的 ViewPager 类,我正在使用onPageScrolled它来捕获偏移量。

protected void onPageScrolled(int position, float offset, int offsetPixels)

onTouchEvent用于控制视图滚动

public boolean onTouchEvent(MotionEvent event) {        
    if (this.isPagingEnabled) {
        return super.onTouchEvent(event);
    }

    return false;
}

我需要在禁用 viewpager 滚动时捕获偏移更改,就好像它正在滚动以显示自定义滚动效果一样onDraw

4

1 回答 1

0

您需要编写自定义 Scroller,您可以在其中更改它的滚动持续时间...

package com.finoit.fsop.widgets;

import java.lang.reflect.Field;

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

public class CustomViewPager extends ViewPager {

    private ScrollerCustomDuration mScroller = null;

    private boolean enabled;

    public CustomViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.enabled = true;
        postInitViewPager();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        if (this.enabled) {
                return super.onTouchEvent(event);
        }

        return false;
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {

        if (this.enabled) {
                return super.onInterceptTouchEvent(event);
        }

        return false;
    }

    public void setPagingEnabled(boolean enabled) {
        this.enabled = enabled;
    } 

    /**
     * Override the Scroller instance with our own class so we can change the duration
     */
    private void postInitViewPager() {

        try {

                Class<?> viewpager = ViewPager.class;
                Field scroller = viewpager.getDeclaredField("mScroller");
                scroller.setAccessible(true);
                Field interpolator = viewpager.getDeclaredField("sInterpolator");
                interpolator.setAccessible(true);

                mScroller = new ScrollerCustomDuration(getContext(), (Interpolator) interpolator.get(null));
                scroller.set(this, mScroller);

        }
        catch (Exception e) {
        }

    }

    /**
     * Set the factor by which the duration will change
     */
    public void setScrollDurationFactor(double scrollFactor) {
            mScroller.setScrollDurationFactor(scrollFactor);
    }


}


package com.finoit.fsop.widgets;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.animation.Interpolator;
import android.widget.Scroller;

public class ScrollerCustomDuration extends Scroller {

    private double mScrollFactor = 1;

    public ScrollerCustomDuration(Context context) {
        super(context);
    }

    public ScrollerCustomDuration(Context context, Interpolator interpolator) {
        super(context, interpolator);
    }

    @SuppressLint("NewApi")
    public ScrollerCustomDuration(Context context, Interpolator interpolator, boolean flywheel) {
        super(context, interpolator, flywheel);
    }

    /**
     * Set the factor by which the duration will change
     */
    public void setScrollDurationFactor(double scrollFactor) {
        mScrollFactor = scrollFactor;
    }

    @Override
    public void startScroll(int startX, int startY, int dx, int dy, int duration) {
        super.startScroll(startX, startY, dx, dy, (int) (duration * mScrollFactor));
    }

}

对于滚动效果

@Overwrite onPageScrolled(int position, float offset, int offsetPixels)
于 2013-09-05T04:28:22.260 回答