Mes*_*Mes 8 android android-recyclerview
我有一些奇怪的问题,RecyclerView因为我更改了我的应用程序以从Room数据库加载数据.
我有一个屏幕(a Fragment),显示用户的配置文件.基本上它是GridLayout第一项(行)显示关于用户的一些信息,然后对于每行我显示三个图像.
代码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);
}
Run Code Online (Sandbox Code Playgroud)
这就是我加载数据的方式:
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 -> {}));
Run Code Online (Sandbox Code Playgroud)
两个调用都返回a Flowable,a Flowable<Account> 或者Flowable<Post>where,Account并且Post是Room的实体类.这两种方法showAccount(),并showPosts()通过前面提到的实体类的适配器.
问题是:在加载数据的那一刻,它会滚动到屏幕的底部(也就是底部RecyclerView).
我不确定为什么会这样.是因为它Room的Flowable类型吗?因为之前当我从网络获取数据并将它们传递给适配器时我没有遇到这个问题.
编辑:我正在使用25.3.1RecyclerView的版本
编辑2:这是我用Account和Post数据更新我的适配器类的方法:
public void addPhotos(List<Post> posts) {
dataset.addAll(posts);
notifyDataSetChanged();
}
public void addProfileItem(Account account) {
this.account = account;
notifyItemInserted(0);
}
Run Code Online (Sandbox Code Playgroud)
Arp*_* J. 17
你试过这个吗?将descendantFocusabilityparent的属性设置RecyclerView为blockDescendants.
这样做,它将不再关注回收器视图中动态加载的子视图,因此不会发生自动滚动.
android:descendantFocusability="blocksDescendants"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4664 次 |
| 最近记录: |