RecyclerView scrolls to bottom when data is loaded

ぃ、小莉子 提交于 2020-12-25 01:51:06

问题


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);
}

回答1:


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.




回答2:


In my case this line was culprit,

GridLayoutManager gridLayoutManager = new GridLayoutManager(this, SPAN_COUNT, GridLayoutManager.VERTICAL, true);

Specifically , last parameter which is setting reverseLayout attribute to true. Make it false and RecyclerView doesn't scroll to bottom of screen.



来源:https://stackoverflow.com/questions/47758084/recyclerview-scrolls-to-bottom-when-data-is-loaded

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!