Save the RecyclerView scroll position using StateRestorationPolicy

Viewed 24

I tried to save and restore the RecyclerView scroll position using StateRestorationPolicy.PREVENT_WHEN_EMPTYbut it doesn't work. Am I doing anything wrong?

    private fun initRecyclerView() {

        val recyclerView = binding.activityMainRecyclerView
        val snap = LinearSnapHelper()
        recyclerView.adapter = movieAdapter
        movieAdapter.stateRestorationPolicy = RecyclerView.Adapter.StateRestorationPolicy.PREVENT_WHEN_EMPTY
        movieAdapter.setOnclickListener(object : MoviesRecyclerAdapter.onItemClickListener {
            override fun onItemClick(position: Int) {
                mylist = viewModel.getPopularMovies.value!!
                val recyclerViewItem = mylist.get(position)
                val intent = Intent(this@StarMovieActivity, DetailsActivity::class.java)
                intent.putExtra("id", recyclerViewItem.id)
                startActivity(intent)
            }

        })
        layoutManager = GridLayoutManager(this, 2)
        recyclerView.layoutManager = layoutManager
        recyclerView.setOnFlingListener(null);
        snap.attachToRecyclerView(recyclerView)

    }
0 Answers
Related