简体   繁体   English

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

[英]How to scroll to a particular position in recyclerview inside scrollview?

I have searched a lot regarding my query and none of them was useful. 我已经搜索了很多关于我的查询,但没有一个是有用的。 I have a recyclerview and some static data inside a scroll view which is inside a root parentlayout as show below. 我在一个滚动视图中有一个recyclerview和一些静态数据,它位于root parentlayout中,如下所示。

I have set - 我已经设定 -

scrollview.scrollto(0,0);

because everytime i open the activity it jumps to the recyclerview firstitem and it skips out the static data above the recyclerview. 因为每当我打开活动时它会跳转到recyclerview firstitem并且它会跳过recyclerview上方的静态数据。

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

for smoothscroll. 对于smoothscroll。

the problem is with- 问题是 -

layoutmanager.scrollToPositionWithOffset(pos,0);

it is not working. 它不起作用。 I have set the aboveline after setting adapter to recyclerview. 我在将适配器设置为recyclerview后设置了aboveline。 Also tried with NestedScrollView but in vain. 也尝试使用NestedScrollView但是徒劳无功。

although I have used 虽然我用过

layoutmanager.scrollToPosition(pos);

For those who skip the code, i have set match_parent to ScrollView and fillviewport to true. 对于那些跳过代码的人,我已将match_parent设置为ScrollView,并将fillviewport设置为true。

Here is my layout. 这是我的布局。

<?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>

Need to scroll ScrollView by getting top position of RecyclerView 's child : 需要通过获取RecyclerView的孩子的最高位置来滚动ScrollView

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

您可以使用滚动查看方法到达所需的位置:

scrollView.smoothScrollTo(int x, int y);

I know I'm answering pretty late but if you get an auto scroll issue, you need to add android:descendantFocusability="blocksDescendants" in your RelativeLayout. 我知道我的回答很晚但如果你遇到自动滚动问题,你需要在RelativeLayout中添加android:descendantFocusability =“blocksDescendants” So, your RealtiveLyout tag will look like this: 因此,您的RealtiveLyout标记将如下所示:

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

Step 1: Add this line for selecting that position in the recyclerview 步骤1:添加此行以在recyclerview中选择该位置

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

Step 2: Once check below constructor by getting current position 第2步:通过获取当前位置检查下面的构造函数

  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;
    }

Step 3: And this function also in adapter 第3步:此功能也适用于适配器

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

Last step 4. Add this function outside adapter in your class 最后一步4.在类中的适配器外添加此功能

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

Pust this line in your listview item click listner 将此行列在listview项目中单击列表器

  ((ClassName) context).onPagerItemClick2(position);

That position will be selected when will you open listview .. 当你打开listview时,将选择该位置..

Use this method scrollToPosition (int position) this is work for me. 使用此方法scrollToPosition(int position)这对我有用 hope this will help you. 希望这会帮助你。

        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);

this is my layoutstructer 这是我的布局结构

在此输入图像描述

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM