android - Recyclerview smoothScrollToPosition 在android中不起作用
问题描述
我在 NestedScrollView 中使用 Recycler 视图,我的代码在这里
<androidx.core.widget.NestedScrollView
android:id="@+id/rvSV"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:scrollbars="none"
tools:ignore="UselessParent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/menu_recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
</androidx.core.widget.NestedScrollView>
使用适配器将值加载到回收器视图后,我需要滚动到特定位置。所以我正在使用
recyclerView.smoothScrollToPosition(scroll_value);
它不工作。所以我使用以下代码滚动到位置。那也行不通。
SectionRecyclerViewAdapter adapter = new SectionRecyclerViewAdapter(getActivity(), sectionModelArrayList, this, bottomNavigation);
recyclerView.setAdapter(adapter);
float y;
if (scroll_value == 0 || scroll_value == 1) {
y = recyclerView.getChildAt(0).getY();
} else {
y = recyclerView.getChildAt(scroll_value - 1).getY();
}
rvSV.postDelayed(new Runnable() {
@Override
public void run() {
rvSV.fling(0);
rvSV.smoothScrollTo(0, (int) y);
}
}, 200);
adapter.notifyDataSetChanged();
谁能告诉我如何解决这个问题?提前致谢。
解决方案
尝试添加recyclerview依赖
dependencies
{
implementation "androidx.recyclerview:recyclerview:1.2.1"
implementation "androidx.recyclerview:recyclerview-selection:1.1.0"
}
推荐阅读
- nginx - 如果url包含特定单词,nginx如何重定向到wordpress文件夹
- node.js - 从 p12 node-forge 获取证书正文和私钥
- amazon-web-services - AWS API 网关 URL 返回 {"message":"Forbidden"}
- php - Dotenv 必须是 Dotenv\Loader 的一个实例
- java - 如何从 MongoDB 和 Cosmos DB 的值字段中获取可嵌入文档?
- .net - PowerShell Active Directory 是否调用 System.DirectoryServices.AccountManagement
- python - Python:搜索和替换 HTML 字符串中的所有 img 标签
- java - java.security.NoSuchAlgorithmException:提供者 SunJCE 不提供 DES/ECB/NoPadding
- linq - 从开始工作日期开始几个月的工作经验 MVC
- javascript - 按属性在对象数组中查找重复项的最短方法