简体   繁体   English

PagerSnapHelper 中的 From-To Page Changed 侦听器

[英]From-To Page Changed Listener in PagerSnapHelper

I'm using PagerSnapHelper in a horizontal RecyclerView to achieve a view pager like behaviour.我在水平RecyclerView中使用PagerSnapHelper来实现类似视图寻呼机的行为。

final PagerSnapHelper pagerSnapHelper = new PagerSnapHelper(); pagerSnapHelper.attachToRecyclerView(recyclerView);

It works great, but I want to be able to get callbacks for when the user changes the page in either direction.它工作得很好,但我希望能够在用户向任一方向更改页面时获得回调。 So something like, onSwipeLeft / onSwipeRight callbacks.所以类似于onSwipeLeft / onSwipeRight回调。

I tried using findTargetSnapPosition in PagerSnapHelper , but that only gives me the targetIndex and not the current index.我尝试在PagerSnapHelper findTargetSnapPosition但这只给我targetIndex而不是当前索引。 I tried something like this, but it doesn't really work all the time.我试过这样的东西,但它并不总是有效。

@Override
public int findTargetSnapPosition(RecyclerView.LayoutManager layoutManager, int velocityX, int velocityY) {
    final int targetPos = super.findTargetSnapPosition(layoutManager, velocityX, velocityY);

    final View currentView = findSnapView(layoutManager);
    final int currentPos = layoutManager.getPosition(currentView);

    if (currentPos < targetPos) {
        callback.onSwipeRight();
    } else if (currentPos > targetPos) {
        callback.onSwipeLeft();
    }

    return targetPos;
}

Is there a better way to achieve this which always works?有没有更好的方法来实现这个总是有效的? Thanks!谢谢!

Update 2更新 2
!! !! Important note !!重要的提示 !!
If you programmatically call scrollTo() and use the SnapPagerScrollListener with ON_SETTLED , onScrollStateChanged won't get called.如果您以编程方式调用scrollTo()并将SnapPagerScrollListenerON_SETTLED一起使用,则不会调用onScrollStateChanged So the old snap position does not get updated.所以旧的捕捉位置不会得到更新。 WIP, will update the class as soon as I fix it. WIP,我会在修复后立即更新课程。

Update更新

The original class had some issues with notifying on first layout.原始类在第一个布局上通知时存在一些问题。 Now it fires only the first time the item position changes from RecyclerView.NO_POSITION to something else.现在它仅在项目位置第一次从RecyclerView.NO_POSITION更改为其他位置时触发。

To further extend to ignore/fire only on user gestures, so non programmatic calls to scrollTo() , note that onScrolled() gets triggered with dx == 0 and dy == 0 in case of a programmatic call.为了进一步扩展到仅在用户手势上忽略/触发,因此对scrollTo()的非编程调用,请注意,在编程调用的情况下, onScrolled()会被dx == 0 and dy == 0触发。

public class SnapPagerScrollListener extends RecyclerView.OnScrollListener {

    // Constants
    public static final int ON_SCROLL = 0;
    public static final int ON_SETTLED = 1;

    @IntDef({ON_SCROLL, ON_SETTLED})
    public @interface Type {
    }

    public interface OnChangeListener {
        void onSnapped(int position);
    }

    // Properties
    private final PagerSnapHelper snapHelper;
    private final int type;
    private final boolean notifyOnInit;
    private final OnChangeListener listener;
    private int snapPosition;

    // Constructor
    public SnapPagerScrollListener(PagerSnapHelper snapHelper, @Type int type, boolean notifyOnInit, OnChangeListener listener) {
        this.snapHelper = snapHelper;
        this.type = type;
        this.notifyOnInit = notifyOnInit;
        this.listener = listener;
        this.snapPosition = RecyclerView.NO_POSITION;
    }

    // Methods
    @Override
    public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
        super.onScrolled(recyclerView, dx, dy);
        if ((type == ON_SCROLL) || !hasItemPosition()) {
            notifyListenerIfNeeded(getSnapPosition(recyclerView));
        }
    }

    @Override
    public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
        super.onScrollStateChanged(recyclerView, newState);
        if (type == ON_SETTLED && newState == RecyclerView.SCROLL_STATE_IDLE) {
            notifyListenerIfNeeded(getSnapPosition(recyclerView));
        }
    }

    private int getSnapPosition(RecyclerView recyclerView) {
        RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
        if (layoutManager == null) {
            return RecyclerView.NO_POSITION;
        }

        View snapView = snapHelper.findSnapView(layoutManager);
        if (snapView == null) {
            return RecyclerView.NO_POSITION;
        }

        return layoutManager.getPosition(snapView);
    }

    private void notifyListenerIfNeeded(int newSnapPosition) {
        if (snapPosition != newSnapPosition) {
            if (notifyOnInit && !hasItemPosition()) {
                listener.onSnapped(newSnapPosition);
            } else if (hasItemPosition()) {
                listener.onSnapped(newSnapPosition);
            }

            snapPosition = newSnapPosition;
        }
    }

    private boolean hasItemPosition() {
        return snapPosition != RecyclerView.NO_POSITION;
    }
}


Usage:用法:
Just add an instance of SnapPagerScrollListener to your RecyclerView只需将 SnapPagerScrollListener 的实例添加到您的 RecyclerView

your_recycler_view.addOnScrollListener(new SnapPagerScrollListener(your_snap_helper, SnapPagerScrollListener.ON_SCROLL/ON_SETTLED, true/false, your_on_changed_listener));


The Type property is for defining when the callbacks should be triggered. Type属性用于定义何时触发回调。

  1. ON_SCROLL: for notifiying the callback as soon as as the new View/Page passes the middle ON_SCROLL:用于在新视图/页面通过中间时立即通知回调
  2. ON_SETTLED: for notifying the callback after the RecyclerViews state is SCROLL_STATE_IDLE . ON_SETTLED:用于通知 RecyclerViews 状态为SCROLL_STATE_IDLE后的回调。 I use the mode for only firing API calls when the scroll has settled.我使用该模式仅在滚动稳定后触发 API 调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM