How to distinguish user scroll from scrollToPosition () in RecyclerView.OnScrollListener?

In my onScrolled()method, RecyclerView.OnScrollListenerI need to distinguish between two different sources of scroll events:

  • User scrolling RecyclerViewthrough touch events
  • Code scrolling RecyclerViewwithscrollToPosition()

There is no parameter onScrolled()that seems to cover this. Is there a recipe for this difference?

FWIW, my scenario: I have two widgets RecyclerView. One of them is a master and shows a traditional vertical list. The other serves as a replacement ViewPagerto allow you to scroll through the details of the same elements that are in the vertical list. In an environment with a large screen, both widgets RecyclerView(template-part template) are visible at the same time , and I need to synchronize them. If the user is viewing the pager RecyclerView, I need to change the indicator in the list of RecyclerViewstrings to match. If the user enters a line in the list RecyclerView, I need to refresh the current page in the pager RecyclerView. To listen to scroll events on a pager, I use OnScrollListener, but it also works when scrolling a pager in response to clicks on a list line.

+4
1

.

, — RecyclerView. , RecyclerViewEx, API- :

public class RecyclerViewEx extends RecyclerView {
  interface OnScrollStateChangedListener {
    void onScrollStateChanged(int state);
  }

  final private ArrayList<OnScrollStateChangedListener> listeners=new ArrayList<>();

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

  public RecyclerViewEx(Context context, @Nullable
    AttributeSet attrs) {
    super(context, attrs);
  }

  public RecyclerViewEx(Context context, @Nullable AttributeSet attrs,
                        int defStyle) {
    super(context, attrs, defStyle);
  }

  public void addOnScrollStateChangedListener(OnScrollStateChangedListener listener) {
    listeners.add(listener);
  }

  public void removeOnScrollStateChangedListener(OnScrollStateChangedListener listener) {
    listeners.remove(listener);
  }

  @Override
  public void onScrollStateChanged(int state) {
    super.onScrollStateChanged(state);

    for (OnScrollStateChangedListener listener : listeners) {
      listener.onScrollStateChanged(state);
    }
  }
}

scrollToPosition() , . , .

rve.addOnScrollStateChangedListener(
  scrollState -> {
    if (scrollState==RecyclerView.SCROLL_STATE_IDLE) {
      // do something cool, now that the user swipe is complete
    }
  });

, - , .

+1

Source: https://habr.com/ru/post/1693276/


All Articles