I have a recyclerView showing a list of users from a Room database.

Dao

    @Query("SELECT * from users_table ORDER BY name COLLATE NOCASE ASC")
    LiveData<List<User_Entity>> getAllUsers();

activity

    RecyclerView mRecyclerView = view.findViewById(R.id.traininglistview);
    usersViewModel = ViewModelProviders.of(this).get(UsersViewModel.class);

    usersViewModel.getAllUsers().observe(this, new Observer<List<User_Entity>>() {
        @Override
        public void onChanged(@Nullable List<User_Entity> user_entities) {
            mUsers = user_entities;
            mAdapter.setUsers(user_entities);
        }
    });

    mAdapter = new UsersAdapter(context, usersViewModel);
    mRecyclerView.setAdapter(mAdapter);
    mLayoutManager = new LinearLayoutManager(context);
    mRecyclerView.setLayoutManager(mLayoutManager);
    ...

The list of users is ordered by the user name.

When I click a user item, a new Activity is launched to allow me to edit user values.

If I change the user name, because my query is ordered by name, the new user recyclerView position is changed.

When returning from user values edit, how can I scroll my recyclerView to show the just edited user? Or, how can I found the new user list position to use mLayoutManager.scrollToPosition?

0 Answers