Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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.setNestedScrollingEnabled(false);  recyclerview.setfocusable(false); 

for smoothscroll.

the problem is with-

layoutmanager.scrollToPositionWithOffset(pos,0); 

it is not working. I have set the aboveline after setting adapter to recyclerview. Also tried with NestedScrollView but in vain.

although I have used

layoutmanager.scrollToPosition(pos); 

For those who skip the code, i have set match_parent to ScrollView and fillviewport to 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> 
like image 509
Aman Verma Avatar asked Nov 23 '16 21:11

Aman Verma


People also ask

How do you make a recycler view scrollable?

To be able to scroll through a vertical list of items that is longer than the screen, you need to add a vertical scrollbar. Inside RecyclerView , add an android:scrollbars attribute set to vertical .

How do you scroll to the recycler view to the bottom?

Recyclerview scroll to bottom using scrollToPositon. After setting the adapter, then call the scrollToPosition function to scroll the recycler view to the bottom. recyclerView. setAdapter(mAdapter); recyclerView.


1 Answers

Need to scroll ScrollView by getting top position of RecyclerView's child :

float y = recyclerView.getY() + recyclerView.getChildAt(selectedPosition).getY();     scrollView.smoothScrollTo(0, (int) y); 
like image 98
Vaibhav Jani Avatar answered Oct 03 '22 10:10

Vaibhav Jani