0

I'm trying to scroll recycler view item to top of screen when user clicks on an item.

My layout is set as following

- <FrameLayout>
--   <ScrollView>
---     <LinearLayout /> // static content
---     <RecyclerView /> // dynamic content
---     <LinearLayout /> // static content
--   </ScrollView>
-- </FrameLayout>

What I want to achieve is when i click on an item of recyclerview. It should scroll up to top of the screen.

I have tried following so far but no luck.

-> 
  // use recycler view's layout manager to scroll.
  recyclerView.layoutManager.scrollToPositionWithOffset(position)

->
 // use smooth scroll to scroll
 recyclerView.smoothScrollToPosition(ItemPos)

->
  // use main scroll view to scroll on the screen. This kind of works but does not move item to top of the page.
  val y = recyclerView.y + recyclerView.getChildAt(position).y
  activity.binding.mainScrollview?.smoothScrollTo(0, y.toInt())

Any help is appreciated. Thank you

Abp
  • 113
  • 5
  • 26
  • What was wrong with? recyclerView.layoutManager.scrollToPositionWithOffset(position) – Steve Moretz Feb 14 '19 at 18:04
  • Did you try a simple : recyclerView.smoothScrollToPosition(ItemPos); – Steve Moretz Feb 14 '19 at 18:06
  • @stevemoretz with recyclerView.layoutManager.scrollToPositionWithOffset(position) it would jump up then back down again. And yes, I have also tried recyclerView.smoothScrollToPosition(ItemPos). It didn't work. – Abp Feb 14 '19 at 18:08
  • Well It didn't work what happened?Are you calling these methods on the click really or just testing them now?If you're testing make sure you call the methods after everything has been setup. – Steve Moretz Feb 14 '19 at 18:33
  • Did you consider another thing.Do you have enough items?So when you click on the third item goes to the top.Remember the last item always sticks to the bottom of recyclerView. – Steve Moretz Feb 14 '19 at 18:36
  • @stevemoretz. I guess I don't have enough items. But any thoughts on how I make the last item scroll up. – Abp Feb 14 '19 at 18:38
  • You should have enough items or this is a special case which can't happen with a normal recyclerview which means you got to do some stuffs.One options is using translationY of the recyclerView.You need some math.Looks like your items are the same size.So Put a listener get the difference of the each scroll and add them up if it got to last item which can't go anymore down you'll add up the translationY so the whole thing goes up.Consider how much you need such a thing extend the recyclerView and make your customView or if you don't know it's ok you can still do it in your activity – Steve Moretz Feb 14 '19 at 18:42
  • Oh there's a simpler way to get it just ocuured to me.Just add a few dummy items you know just add more items with width and height of the normal items but they have nothing in them and they're transparent.You get everything working make sure you won't put any click listener on these items :). You're good to go.If you agree I'll post the best solution as the answer. – Steve Moretz Feb 14 '19 at 18:45
  • @stevemoretz yeah that makes sense and should work. Thank you – Abp Feb 14 '19 at 18:47
  • You're welcome I post an answer you can accept and upvote now. – Steve Moretz Feb 14 '19 at 18:51

2 Answers2

2

Looks like in your case you don't have enough items.

You can't get recyclerView scroll lower than the last Item So it's either you make your own implementation of recyclerView by extending it,get add scroll amounts and when got to the last item you'll add up the translationY of the recyclerView.

Or you can add a few dummy Items.So they can be View with width and height of your normal Items.That should do it.

Steve Moretz
  • 2,758
  • 1
  • 17
  • 31
0

Have you tried layoutManager's scrollToPositionWithOffset function:

thirdItem.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            LinearLayoutManager layoutManager = (LinearLayoutManager) mRecyclerView
                    .getLayoutManager();
            layoutManager.scrollToPositionWithOffset(0, 0);
        }
});
Renaissance
  • 564
  • 5
  • 26