如何滚动到scrollview内的recyclerview中的特定位置?

Sni*_*per 29 android scrollview android-recyclerview linearlayoutmanager android-nestedscrollview

我已经搜索了很多关于我的查询,但没有一个是有用的.我在一个滚动视图中有一个recyclerview和一些静态数据,它位于root parentlayout中,如下所示.

我已经设定 -

scrollview.scrollto(0,0);
Run Code Online (Sandbox Code Playgroud)

因为每当我打开活动时它会跳转到recyclerview firstitem并且它会跳过recyclerview上方的静态数据.

recyclerview.setNestedScrollingEnabled(false); 
recyclerview.setfocusable(false);
Run Code Online (Sandbox Code Playgroud)

对于smoothscroll.

问题是 -

layoutmanager.scrollToPositionWithOffset(pos,0);
Run Code Online (Sandbox Code Playgroud)

它不起作用.我在将适配器设置为recyclerview后设置了aboveline.也尝试使用NestedScrollView但是徒劳无功.

虽然我用过

layoutmanager.scrollToPosition(pos);
Run Code Online (Sandbox Code Playgroud)

对于那些跳过代码的人,我已将match_parent设置为ScrollView,并将fillviewport设置为true.

这是我的布局.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/bottomsheet"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.inkdrops.khaalijeb.BrandCouponActivity">

    <RelativeLayout
        android:id="@+id/inclusionviewgroup"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <static data/>

        <ScrollView
            android:id="@+id/scrollviewmain"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/one"
            android:fillViewport="true"
            android:scrollbars="none"
            android:layout_above="@+id/donelayout">


            <staticdata/>

                <TextView
                    android:id="@+id/dealstext"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/card1"
                    android:layout_marginTop="10dp"
                    android:layout_marginLeft="10dp"
                    android:textSize="16sp"
                    android:text="Deals &amp; Coupons"
                    android:textColor="#444444" />

                <RelativeLayout
                    android:id="@+id/recyclerlayout"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/dealstext"
                    android:layout_marginTop="5dp"
                    android:layout_marginLeft="8dp"
                    android:layout_marginRight="8dp"
                    android:background="@color/colorbackground">

                    <android.support.v7.widget.RecyclerView
                        android:id="@+id/coupon_rec_view"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:background="@color/colorbackground"
                        android:visibility="visible"/>


                </RelativeLayout>

                <statisdata>


            </RelativeLayout>

        </ScrollView>

        include layout="@layout/activity_coupons"
            android:visibility="gone"
            />

    </RelativeLayout>

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

Vai*_*ani 32

需要ScrollView通过获得RecyclerView孩子的最高位置滚动:

float y = recyclerView.getY() + recyclerView.getChildAt(selectedPosition).getY();    
scrollView.smoothScrollTo(0, (int) y);
Run Code Online (Sandbox Code Playgroud)

  • @msrowley如果您的RecyclerView不是XML文件中的第一个元素,则将RecyclerView的Y位置添加到子项的Y位置. (3认同)