Search code examples
androidandroid-recyclerviewlinearlayoutmanager

how to let item scroll to top of the Recycleview when using LinelayoutManager.scrollToPositionWithOffset?first time not effect


i want to let the recycleview item scroll to top by click one button.and i use lineLayoutManger.scrollToPositionWithOffset(position,0); the position is i want to scroll item postion.but when i go into the Activity and have init the adapter.but when i click the buttton to let fifth item scroll to top. but it just on bottom of the screen. if i click the button again it show normal the fifth item is on top.the items are not the same height. whate should i do.i have search many article.but have no answer.thanks the wrong effect picturesee this picture


Solution

  • get linearLayoutManager from your recyclerView List like this:

    ((LinearLayoutManager) mList.getLayoutManager()).scrollToPositionWithOffset(mPositionToScroll, 0);