如何使RecyclerView滚动到onCreate的最后一个位置?

时间:2018-12-22 01:30:58

标签: android android-recyclerview viewmodel smooth-scrolling

当片段开始时,我只是试图将RecyclerView移到最后一个位置,但是找不到解决方案。当在数据库中插入新的“ Pagina”时,下面的代码可以正常工作,它将移动到最后一个位置。但是,如果我关闭然后重新打开,它将回到顶部。那么,如何在创建片段时使平滑滚动工作?

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    appDatabase = AppDatabase.getDatabase(requireContext())

    val smoothScroller = object : LinearSmoothScroller(context) {
        override fun getVerticalSnapPreference(): Int = SNAP_TO_START
    }
    viewModelo = ViewModelProviders.of(this).get(ViewModelo::class.java)
    // Create the observer which updates the UI.
    val listPaginas = Observer <List<Pagina>> {
        // Update the UI, in this case, a TextView.

        recyclerViewAdapter?.addItens(it)  
            if (it.size < 1) {               
            smoothScroller.targetPosition = it.size
            } else {
               smoothScroller.targetPosition = it.size-1
        }      

        recyclerView?.layoutManager?.startSmoothScroll(smoothScroller)
    }

    viewModelo?.todasPaginas()?.observe(this, listPaginas)

}

1 个答案:

答案 0 :(得分:1)

尝试以下更改,它可以正常工作!

A.CallTo(() => fakeFoo.AccessTheWebAsync2(
     A<List<SpecialString>>.That.IsSameSequenceAs(result)))
  .Returns(5);