简体   繁体   English

Android-回收站视图滚动到位置不起作用

[英]Android - recycler view scroll to position doesn't work

I am trying to retrieve data from firebase then display it in recycler view. 我正在尝试从Firebase检索数据,然后将其显示在回收站视图中。 I set the layout to be reverselayout = true. 我将布局设置为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. 如下面所注释的,我尝试使用scrollToPosition运行应用程序时更改了初始位置,但是仍然没有任何变化。 Does anyone have a solution related to this problem? 有没有人有与此问题相关的解决方案?

I've tried this: https://stackoverflow.com/a/26876044/7825519 But still start from bottom. 我已经尝试过: https : //stackoverflow.com/a/26876044/7825519但还是从下开始。

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))

First of all delete the both lines that make the recyclerview start from the botom. 首先,删除使recyclerview从底部开始的两条线。 Set the scrolling after you set the adapter: 设置适配器后设置滚动:

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

尝试使用View post方法设置scrollToPosition

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM