0

I am trying to retrieve data from firebase then display it in recycler view. I set the layout to be reverselayout = true. However, when I run the activity, the view starts at the bottom. I tried to change the initial position when the application is run using scrollToPosition as commented below, but still nothing changes. Does anyone have a solution related to this problem?

I've tried this: https://stackoverflow.com/a/26876044/7825519 But still start from bottom.

mRecyclerView!!.layoutManager = LinearLayoutManager(this, RecyclerView.VERTICAL, false)
    (mRecyclerView!!.layoutManager as LinearLayoutManager).reverseLayout = true
    (mRecyclerView!!.layoutManager as LinearLayoutManager).stackFromEnd = true
    mRecyclerView?.scrollToPosition(3)
//        mRecyclerView?.smoothScrollToPosition(3)
//        (mRecyclerView!!.layoutManager as LinearLayoutManager).scrollToPositionWithOffset(3,0)
//        mNestedScrollView?.scrollTo(0,3)
    mAdapter = DiscoverAdapter(mItems, mPostKey, Static.mLikedPosts, this)
    mRecyclerView!!.adapter = mAdapter
    val spacingInPixels = resources.getDimensionPixelSize(R.dimen.margin_between_card)
    mRecyclerView!!.addItemDecoration(SpacesItemDecoration(spacingInPixels))
mrtn_24
  • 73
  • 9
  • You might want to look at this: https://android-pratap.blogspot.com/2015/01/recyclerview-with-checkbox-example.html – Dinesh Neupane Jul 10 '18 at 09:20
  • Do you want to start from the bottom? Because this is what you're asking with reverseLayout=true and stackFromEnd = true. –  Jul 10 '18 at 09:20
  • 1
    Possible duplicate of [Scroll RecyclerView to show selected item on top](https://stackoverflow.com/questions/26875061/scroll-recyclerview-to-show-selected-item-on-top) – Sphinx Jul 10 '18 at 09:22
  • @mTak I want to start from a position other than the bottom position – mrtn_24 Jul 10 '18 at 09:27
  • Why do you set reverseLayout=true and stackFromEnd = true? –  Jul 10 '18 at 09:28
  • @Sphinx I've tried that before. But still doesn't work. – mrtn_24 Jul 10 '18 at 09:28
  • So comment out these 2 lines and try again –  Jul 10 '18 at 09:29
  • @mTak I try stackFromEnd = true and false nothing happen. I use reverselayout = true to reverse the order of my data from firebase. It works, but the scroll position starts from bottom. I want to change it from the other position – mrtn_24 Jul 10 '18 at 09:34
  • see my answer. I did not see that that you set the adapter after you try to scroll –  Jul 10 '18 at 09:35

2 Answers2

0

First of all delete the both lines that make the recyclerview start from the botom. Set the scrolling after you set the adapter:

mRecyclerView!!.adapter = mAdapter
mRecyclerView?.scrollToPosition(3)
0

Try to set scrollToPosition using post method of View