如何在ScrollView内部的RecyclerView中滚动到特定位置?

32

我对我的问题进行了很多搜索,但没有一个对我有用。我有一个RecyclerView和一些静态数据在ScrollView中,ScrollView又位于根ParentLayout中,如下所示。

我已经设置 -

scrollview.scrollto(0,0);

每次我打开这个活动时,它都会跳转到RecyclerView的第一个项目,并跳过RecyclerView上面的静态数据。

recyclerview.setNestedScrollingEnabled(false); 
recyclerview.setfocusable(false);

平滑滚动的问题。

问题出在 -

layoutmanager.scrollToPositionWithOffset(pos,0);

它没有起作用。在将适配器设置到recyclerview之后,我已经设置了上述线。还尝试过使用NestedScrollView但也没用。

尽管我已经使用了

layoutmanager.scrollToPosition(pos);

对于那些跳过代码的人,我已将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>

你能发布一些你的Android代码吗?你确定你引用的是正确的“layoutManager”吗?不是“RecyclerView”的,而是“ScrollView”的对吧? - Felix Guo
您是想在旋转屏幕时保持滚动位置,还是想通过编程滚动到某个特定位置? - akshay_shahane
请粘贴您的适配器和相关的Activity/Fragment代码,@akshay_shahane。 - Radhey
4个回答

36

需要通过获取RecyclerView子项的顶部位置来滚动ScrollView

float y = recyclerView.getY() + recyclerView.getChildAt(selectedPosition).getY();    
scrollView.smoothScrollTo(0, (int) y);

X是零速吗? - danny117
@danny117 不是序号,而是滚动条在x轴上的位置,即水平滚动值。 (https://developer.android.com/reference/android/widget/ScrollView.html) - Vaibhav Jani
在我的情况下,scrollView正在移动,但它没有到达指定的适配器位置。有什么想法吗? - marios-codes
@msrowley 调试你正在滚动到的位置 - Vaibhav Jani
3
如果你的RecyclerView不是XML文件中的第一个元素,那么需要将RecyclerView的Y位置添加到子项的Y位置。 - user1025013
显示剩余2条评论

0

您可以使用滚动视图方法来到达您想要的位置,如下所示:

scrollView.smoothScrollTo(int x, int y);

-2

步骤1:添加此行以选择recyclerview中的该位置

  adaptercat.setSelectedItem(Integer.parseInt(dealtypeid));

步骤二:通过获取当前位置来检查下面的构造函数

  public CatgerotyAdapter(Context context, ArrayList<HashMap<String, String>> arraylist,String selectedPosition) {
        this.context = context;
        this.data = arraylist;
        resultp = new HashMap<>();
        currentItemPos=Integer.parseInt(selectedPosition)-1;
    }

第三步:这个函数也在适配器中

  public void setCurrentItem(int item)
    {
        this.currentItemPos = item;
    }

最后一步 4. 在你的类中适配器之外添加这个函数

 private void onPagerItemClick2(Integer tag)
{
    adaptercat.setCurrentItem(tag);
    catsp.setAdapter(adaptercat);
}

将此行代码放入您的ListView项单击侦听器中。
  ((ClassName) context).onPagerItemClick2(position);

当你打开列表视图时,该位置将被选中。


-3
使用此方法scrollToPosition (int position) 对我有用,希望对您有所帮助。
        mMessagesRecyclerView = (RecyclerView) findViewById(R.id.messagesRecyclerView);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setStackFromEnd(true);

        mMessagesRecyclerView.setLayoutManager(layoutManager);
        mMessagesAdapter = new MessagesAdapter();
        mMessagesRecyclerView.setAdapter(mMessagesAdapter);

        mMessagesRecyclerView.scrollToPosition(your position);

这是我的布局结构

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接