android - 使用 Recycler View 将房间数据库滚动到特定位置
问题描述
我正在使用 Room 数据库从数据库中获取数据。Recyclerview 我想滚动到recyclerview 中的特定位置。
我已经尝试过 list.setNestedScrollingEnabled(true)
StationListRepository stationListRepositoryOnlyActive = new StationListRepository(getApplicationContext()); stationListRepositoryOnlyActive.getActiveStationList().observe(this, new Observer>() { @Override public void onChanged(List OnlyActivestations) { Log.d("OnlyActivestations", "" + OnlyActivestations.size()); assert OnlyActivestations != null; if (OnlyActivestations.size() > 0 || FinalList.size() > 0) { if (FinalList != null && FinalList.size() > 0) { OnlyActivestations = sortList(OnlyActivestations); FinalList.addAll(OnlyActivestations);
} else {
OnlyActivestations = sortList(OnlyActivestations);
FinalList = OnlyActivestations;
}
Log.d("FinalListSizeActive",""+FinalList.size());
detailsAdapter = new db_stationListAdapter(context, FinalList);
//recyclerView.setNestedScrollingEnabled(true);
StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL);
staggeredGridLayoutManager.scrollToPosition(10);
recyclerView.setLayoutManager(staggeredGridLayoutManager);
recyclerView.setAdapter(detailsAdapter);
detailsAdapter.notifyDataSetChanged();
runOnUiThread(new Runnable() {
@Override
public void run() {
// stopLoading();
stopAnimation();
}
});
}
}
});
我除了回收站视图自动滚动到位置 10
解决方案
将数据设置到适配器后尝试使用下面的代码
recyclerView.smoothScrollToPosition(10);
或者
您可以将OnLayoutChangeListener添加到 RecyclerView。
recyclerView.addOnLayoutChangeListener(new View.OnLayoutChangeListener(){
@Override
public void onLayoutChange(View v, int left, int top, int
right, int bottom, int oldLeft, int oldTop, int oldRight,
int oldBottom) {
if (bottom < oldBottom) {
recyclerView.postDelayed(new Runnable() {
@Override
public void run() {
recyclerView.smoothScrollToPosition(10);
}
}, 100);
}
}
});
推荐阅读
- c - 如何在c中将单词与文件中的数字分开
- react-native - 反应:无法获得本地颁发者证书
- r - 在 ggplot 背景中插入多个带注释的多边形
- mysql - 将 SQL 查询转换为查询构建器 Laravel
- sql - 如何跨关联创建单个查询
- mysql - 如何在不使用 percona 的情况下使用 LSN 在 MySQL 中使用 ansible 进行增量备份?
- javascript - ImmutableJS 记录合并
- javascript - 正在重新安装多个路由中使用的相同组件 react-router
- keycloak - 扩展 Keycloak 身份验证 SPI
- git - git rebase 分支之间的子文件夹