Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RecyclerView scrolls to bottom when data is loaded

I have some weird issue with RecyclerView since I changed my app to load the data from Room database.

I have a screen (a Fragment) which displays a Profile for a user. Basically it's a GridLayout with the first item (row) displaying some info about the user and then for each row I'm displaying three images.

The code for the RecyclerView is :

private void initializeRecyclerView() {
    if (listAdapter == null) {
        listAdapter = new PhotosListAdapter(getActivity(), userProfileAccountId, false);
    }

    rvPhotosList.setNestedScrollingEnabled(true);
    rvPhotosList.getItemAnimator().setChangeDuration(0);

    GridLayoutManager layoutManager = new GridLayoutManager(getContext(), ProfileFragment.GRID_COLUMNS,
            LinearLayoutManager.VERTICAL, false);

    layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
        @Override
        public int getSpanSize(int position) {
            // 3 spans for Header, 1 for photos
            return listAdapter.isHeader(position) ? 3 : 1;
        }
    });

    rvPhotosList.setLayoutManager(layoutManager);

    int spacingInPixels = 1dp;
    rvPhotosList.addItemDecoration(new PaddingItemDecoration(GRID_COLUMNS, spacingInPixels, true));

    rvPhotosList.setAdapter(listAdapter);
}

and this is how I load data :

    compositeDisposable.add(
        repository.getAccount(accountId)
            .doOnNext(account -> {
                if (account != null && view != null) {
                    view.showCover(account.getCover());
                    view.showAccount(account);
                }
            })
            .flatMap(s -> repository.getUserPosts(accountId))
            .subscribe(posts -> {
                if (view != null) {
                    view.showPosts(posts);
                }
            }, throwable -> {}));

Both calls return a Flowable, either a Flowable<Account> or a Flowable<Post> where Account and Post are Room's Entity classes. The two methods showAccount() and showPosts() pass the previously mentioned entity classes to the adapter.

The problem is this : the moment the data are loaded it scrolls to the bottom of the screen (which is also the bottom of the RecyclerView).

I'm not sure why this happens. Is it because of Room's Flowable type?Cause previously when I was fetching the data from network and passing them to the adapter I didn't have this problem.

Edit : I'm using version 25.3.1 for RecyclerView

Edit 2 : This is how I'm updating my adapter class with Account and Post data :

public void addPhotos(List<Post> posts) {
    dataset.addAll(posts);
    notifyDataSetChanged();
}

public void addProfileItem(Account account) {
    this.account = account;
    notifyItemInserted(0);
}
like image 962
Mes Avatar asked Dec 11 '17 17:12

Mes


1 Answers

Did you try this? Setting descendantFocusability property of parent RecyclerView to blocksDescendants.

What this will do is, it will no more focus on your dynamically loaded child views inside recycler view, as a result, the automatic scroll will not take place.

android:descendantFocusability="blocksDescendants"

Note: The property descendantFocusability can be used with other views as well like FrameLayout, RelativeLayout, etc. So if you set this property to your parent/root layout, it will no more scroll to bottom.

like image 165
Arpit J. Avatar answered Oct 14 '22 02:10

Arpit J.