【发布时间】:2020-05-17 05:30:17
【问题描述】:
我实现了 RecyclerView。我想要一个视图滚动到 RecyclerView 中第 5 位的项目顶部。 scrollToPosition(5) 不工作
XML 文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="128dp"
android:orientation="vertical">
<androidx.core.widget.NestedScrollView
android:id="@+id/nestedScrollView_mark"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/linearLayout_markView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView_marks"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="1dp" />
<LinearLayout
android:id="@+id/linearLayout_behavior"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:background="@drawable/layout_bg"
android:orientation="vertical"
android:visibility="invisible">
在片段中:
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.getLayoutManager().scrollToPosition(5);
recyclerView.setAdapter(adapter);
【问题讨论】:
-
您可以通过保存点击位置来处理它,在 onResume() 中您可以执行 scrollToPosition。添加您的编码部分以供单击,以便任何人都可以查看它。
标签: java android android-recyclerview android-nestedscrollview