0

I create parallax effect moving the image background depends on location of first element of recycler view only.

 @Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
     super.onScrolled(recyclerView, dx, dy);
     if ((holder = recyclerView.findViewHolderForAdapterPosition(0)) != null) {
           int offset =  recyclerView.findViewHolderForAdapterPosition(0).itemView.getTop() / 10;
           backgroundPhoto.setTop(offset);
}

The problem is : when the first item of recycler(header) scroll off the screen, background picture somehow jump ot initial position.

1 Answers1

0

Once the view is scrolled off the screen the RecyclerView might still be able to access it but it's getTop() value will have some random value or 0 which will cause your parallax effect to jump.

You could keep a field in your class which saves the currently "scrolled distance" and add dx to it in the onScrolled(...) callback and use this value to calculate the parallax offset.

Mauin
  • 483
  • 3
  • 12