简体   繁体   English

LinearLayout 和 NestedScrollView 中的 RecyclerView - 如何滚动到某个位置的项目顶部?

[英]RecyclerView inside LinearLayout and NestedScrollView - How to scroll to top of item on a certain position?

I implemented RecyclerView.我实现了 RecyclerView。 I would like a view scroll to top of item on 5th position in RecyclerView.我想要一个视图滚动到 RecyclerView 中第 5 个位置的项目顶部。 scrollToPosition(5) don't work scrollToPosition(5)不起作用

XML file: 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">

in Fragment:在片段中:

recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.getLayoutManager().scrollToPosition(5);
recyclerView.setAdapter(adapter);

The scrollToPosition() needs to be used with any of the following ways to restore the scroll position to the selected position.You can save the selected position in onPause() and then restore subsequently. scrollToPosition()需要与以下任意一种方式一起使用,才能将滚动位置恢复到选中的位置。可以在onPause()保存选中的位置,然后再恢复。 There might be two useful ways :-可能有两种有用的方法:-

  1. Using onSaveInstanceState() function to save the state as explained here .使用onSaveInstanceState()函数保存状态说明这里
  2. Using Shared Prefereneces to store the position, and restoring the state after the onCreateView .使用 Shared Prefereneces 存储位置,并在onCreateView之后恢复状态。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Recyclerview 中,如何将初始滚动位置更改为第一项的顶部? - In Recyclerview, how to change initial scroll position to the top of first item? 如何在RecyclerView中执行从顶部到某个位置的滚动项目? - How in RecyclerView perform scroll item with some position to top? Android RecyclerView滚动和位置项位于顶部 - Android RecyclerView scroll and position item at top NestedScrollView内的RecyclerView比RecyclerView滚动得更快 - RecyclerView inside NestedScrollView scroll faster than RecyclerView 检查nestedscrollview中的recyclelerview滚动状态 - Check recyclerview scroll state inside nestedscrollview NestedScrollView中的RecyclerView - 不需要的滚动开始 - RecyclerView inside NestedScrollView - unwanted scroll to begin 在nestedscrollview的recyclerview内部的recyclerview的notifyDataChanged上,外部recyclerview滚动到顶部 - On notifyDataChanged of recyclerview inside a recyclerview in a nestedscrollview the outer recyclerview scrolls to top 滚动到回收站视图中下一个项目的顶部 - Scroll to top of next item in a recyclerview 如何在 NestedScrollView 中限制 RecyclerView 的高度 - How to limit the height of a RecyclerView inside a NestedScrollView NestedScrollView里面没有使用Recyclerview - NestedScrollView not fling with Recyclerview inside
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM