加载数据时,RecyclerView滚动到底部 [英] RecyclerView scrolls to bottom when data is loaded

查看:179
本文介绍了加载数据时,RecyclerView滚动到底部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

RecyclerView 有一些奇怪的问题,因为我更改了应用程序以从 Room 数据库中加载数据.

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

我有一个屏幕( Fragment ),其中显示了用户的个人资料.基本上,这是一个 GridLayout ,其中第一项(行)显示有关用户的一些信息,然后对于每一行,我将显示三张图像.

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.

RecyclerView 的代码是:

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

两个呼叫都返回一个 Flowable ,一个 Flowable< Account> 或一个 Flowable< Post> ,其中 Account Post 是Room的Entity类.两种方法 showAccount() showPosts()将前面提到的实体类传递给适配器.

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.

问题是这样的:加载数据的那一刻,它滚动到屏幕底部(也是 RecyclerView 的底部).

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).

我不确定为什么会这样.是由于 Room 的Flowable类型引起的吗?以前是因为我从网络中获取数据并将它们传递给适配器时,我没有这个问题.

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.

我正在使用版本 25.3.1 用于RecyclerView

Edit : I'm using version 25.3.1 for RecyclerView

这就是我使用 Account Post 数据更新适配器类的方式:

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

推荐答案

您尝试过吗?将父 RecyclerView descendantFocusability 属性设置为 blocksDescendants .

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"

注意:属性 descendantFocusability 可以与其他视图(例如FrameLayout,RelativeLayout等)一起使用.因此,如果将此属性设置为父级/根级布局,它不会再滚动到底部.

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.

这篇关于加载数据时,RecyclerView滚动到底部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆