Android RecyclerView and addOnScrollListener

135 views Asked by At

This is My file where I want to fetch position on screen rotation. and my saveInstance state But I am Facing a logical error while the screen is rotated I am getting a position for first but addOnScrollListener making it 0 and automatically called.

if(savedInstanceState != null) {
            position = savedInstanceState.getInt("Position");
            recyclerView.scrollToPosition(position);
        }

recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView,newState);
                position = linearLayoutManager.findFirstVisibleItemPosition();
            }
        });
 
    @Override
    public void onSaveInstanceState(@NonNull Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt("Position", position);
    }
0

There are 0 answers