0

What is the best way to stick the content of a recyclerview items to the side of the screen. So that when the element goes off screen its content sticks to the side and goes with its parent only after there is no room in the rest of the element for that child.

I've attached the example where user scrolls left, so the red child of rv item is sticked to the left side of the screen.

result

Roman
  • 803
  • 2
  • 10
  • 17
  • I couldn't catch you... Are you expecting like viewpager something.... Share sample code... – Gobu CSG Jul 06 '22 at 19:11
  • @GobuCSG Where do u see here smth about viewpager logic ? It's just usual recyclerview with a lot of items but item content is sticking to the screen sides. I'm asking about approaches/advices. I don't have a sample code. – Roman Jul 06 '22 at 19:29
  • I got it... Use the below answer – Gobu CSG Jul 06 '22 at 20:38

1 Answers1

1

Try this code in your view holder

Kotlin

itemView.viewTreeObserver.addOnScrollChangedListener {
        val offset  = -itemView.left.toFloat()
        YOUR_RED_VIEW.translationX = offset.coerceAtLeast(0f)
//ELEVATION
        YOUR_RED_VIEW.translationZ = if (offset < 0) 2f else 0f
      
    }

Java

itemView.getViewTreeObserver().addOnScrollChangedListener(() -> {
        float offset = (float) -itemView.getLeft();
        YOUR_RED_VIEW.setTranslationX((float) Math.max(offset,0f));
        //ELEVATION
        YOUR_RED_VIEW.setTranslationZ(offset < 0 ? 2f : 0);
    });
Gobu CSG
  • 653
  • 5
  • 7