11

It is work without NestedScrollView but when I used that scroll to position not working with RecyclerView

Below is xml code;

<android.support.design.widget.CoordinatorLayout     
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/co_ordinate_layout"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:background="@color/light_gray">

 <android.support.v4.widget.NestedScrollView    
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:layout_marginTop="@dimen/dimen_55"
  android:fillViewport="true">
<LinearLayout
    android:id="@+id/scrollView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:descendantFocusability="blocksDescendants"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:focusableInTouchMode="true"
        android:orientation="vertical">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <ImageView
                android:id="@+id/img_resturant"
                android:layout_width="match_parent"
                android:layout_height="150dp"
                android:background="@drawable/products_offers"
                android:scaleType="fitXY" />

            <LinearLayout
                android:id="@+id/llTransparent"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/dimen_20"
                android:focusableInTouchMode="true"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/tvRestaurantName"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginTop="@dimen/dimen_15"
                    android:textColor="@color/white"
                    android:textSize="@dimen/sp_20" />

                <TextView
                    android:id="@+id/tvAddress"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:layout_gravity="center"
                    android:layout_toRightOf="@+id/ivTime"
                    android:textColor="@color/white"
                    android:textSize="@dimen/sp_14" />


                <TextView
                    android:id="@+id/tvTime"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:layout_gravity="center"
                    android:textColor="@color/white"
                    android:textSize="@dimen/sp_14" />

                <View
                    android:layout_width="match_parent"
                    android:layout_height="0.5dp"
                    android:layout_marginLeft="@dimen/_20sdp"
                    android:layout_marginRight="@dimen/_20sdp"
                    android:layout_marginTop="@dimen/_10sdp"
                    android:background="@color/white" />

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="35dp"
                    android:layout_marginBottom="@dimen/_10sdp"
                    android:layout_marginTop="@dimen/_3sdp"
                    android:orientation="horizontal"
                    android:weightSum="1">

                    <RelativeLayout
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="0.5">

                        <TextView
                            android:id="@+id/tvRatting"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_centerInParent="true"
                            android:layout_centerVertical="true"
                            android:textColor="@color/white"
                            android:textSize="@dimen/sp_19" />

                    </RelativeLayout>

                    <View
                        android:layout_width="0.5dp"
                        android:layout_height="match_parent"
                        android:layout_marginBottom="@dimen/dimen_5"
                        android:layout_marginTop="@dimen/dimen_5"
                        android:background="@color/white" />

                    <RelativeLayout
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="0.5">

                        <ImageView
                            android:id="@+id/ivCall"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_centerInParent="true"
                            android:layout_centerVertical="true"
                            android:layout_marginBottom="@dimen/dimen_5"
                            android:layout_marginTop="@dimen/dimen_5"
                            android:src="@drawable/call" />

                    </RelativeLayout>
                </LinearLayout>


            </LinearLayout>

        </RelativeLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="@dimen/_15sdp"
            android:layout_marginRight="@dimen/_15sdp"
            android:layout_marginTop="@dimen/dimen_20"
            android:background="@color/white"
            android:elevation="@dimen/_3sdp"
            android:orientation="vertical"
            android:visibility="gone">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginBottom="@dimen/_10sdp"
                android:layout_marginTop="@dimen/_10sdp"
                android:gravity="center_horizontal"
                android:orientation="vertical">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textColor="@color/black"
                    android:textSize="@dimen/sp_20"
                    android:textStyle="bold" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="@dimen/_10sdp"
                    android:textColor="@color/gray"
                    android:textSize="@dimen/sp_13" />

                <!--<TextView-->
                <!--android:id="@+id/tvAddress"-->
                <!--android:layout_width="wrap_content"-->
                <!--android:layout_height="wrap_content"-->
                <!--android:layout_marginTop="@dimen/_5sdp"-->
                <!--android:textColor="@color/black"-->
                <!--android:textSize="@dimen/sp_15" />-->

            </LinearLayout>

        </LinearLayout>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="@dimen/dimen_13"
            android:layout_marginTop="@dimen/dimen_13"
            android:gravity="center"
            android:visibility="gone">

            <ImageView
                android:id="@+id/ivTime"
                android:layout_width="20dp"
                android:layout_height="20dp"
                android:layout_centerVertical="true"
                android:src="@drawable/clock_1"
                android:tint="#F15d36" />

        </RelativeLayout>

    </LinearLayout>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rvSpecialMenu"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:visibility="gone"
        android:nestedScrollingEnabled="false"
        android:layout_marginTop="@dimen/dimen_5" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rvMenu"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/dimen_5"
        android:focusable="false"
        android:nestedScrollingEnabled="false"
        android:background="@color/white"
        android:paddingBottom="@dimen/dimen_50" />

    </LinearLayout>

   </android.support.v4.widget.NestedScrollView>

  <RelativeLayout
android:id="@+id/rlCartBtn"
android:layout_width="match_parent"
android:layout_height="@dimen/dimen_40"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_gravity="bottom|center"
android:background="@color/darkorange"
android:paddingLeft="@dimen/dimen_5"
android:paddingRight="@dimen/dimen_5">

<TextView
    android:id="@+id/tvCartDesc"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:textColor="@color/white"
    android:textSize="@dimen/sp_18"
    android:textStyle="bold" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true"
    android:layout_toLeftOf="@+id/ivCartIcon"
    android:paddingRight="@dimen/dimen_50"
    android:text="View Cart"
    android:textColor="@color/white"
    android:textSize="@dimen/sp_18"
    android:textStyle="bold" />

<ImageView
    android:id="@+id/ivCartIcon"
    android:layout_width="30dp"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_marginRight="@dimen/dimen_5"
    android:src="@drawable/shopping_cart"
    android:tint="@color/white" />


  </RelativeLayout>

<RelativeLayout
android:id="@+id/rlMenuOrangeIcon"
android:layout_width="@dimen/dimen_40"
android:layout_height="@dimen/dimen_30"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_gravity="bottom|center"
android:layout_marginBottom="@dimen/dimen_50"
android:layout_marginRight="@dimen/_15sdp"
android:background="@color/gray">

<ImageView
    android:id="@+id/ivMenu"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic__menu_restaurant" />

</RelativeLayout>

</android.support.design.widget.CoordinatorLayout>

and below is code for scroll to position;

@Override
public void Item(final int pos, View view) {
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
    @Override
    public void run() {
        rvMenu.scrollToPosition(pos);

        Log.e("pos",""+pos);
    }
}, 100);

dialog.dismiss();
}
Vishal Vaishnav
  • 3,346
  • 3
  • 26
  • 57

1 Answers1

45

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

recyclerView.post(() -> {
     float y = recyclerView.getY() + recyclerView.getChildAt(selectedPosition).getY();    
     scrollView.smoothScrollTo(0, (int) y);
});
Ahamadullah Saikat
  • 4,437
  • 42
  • 39
himangi
  • 788
  • 4
  • 9
  • Hii @Himangi ,it is working only with down scroll..its not working with up scroll – Vishal Vaishnav May 16 '18 at 10:21
  • Can you give me better solution? – Vishal Vaishnav May 16 '18 at 10:24
  • 1
    RecyclerView.SmoothScroller smoothScroller = new LinearSmoothScroller(context) { @Override protected int getVerticalSnapPreference() { return LinearSmoothScroller.SNAP_TO_START; } }; -- apply smotth scroller in your layout manager layoutManager.startSmoothScroll(smoothScroller); Now you set the position where you want to scroll to smoothScroller.setTargetPosition(position); – himangi May 16 '18 at 10:51
  • 2
    My app getting crashed...It gives me invalid target position – Vishal Vaishnav May 16 '18 at 10:59
  • Thanks a lot, exactly what I needed to make my RecyclerView scrollTo a certain position inside a NestedScrollView. – ice_chrysler Nov 12 '19 at 16:29
  • Thank you so much, this is what I want to achieve with RecyclerView inside NestedScrollView. – Parth Patel Dec 18 '19 at 11:32
  • Thank you so much, exactly what I'm looking for. – Addy Sep 11 '20 at 10:14
  • Thanks! I've trying to solve this problem for days. – MadMax Jan 14 '22 at 00:37
  • @himangi the solution is working fine, I have a custom view inside that position how to scroll to that custom view inside that position? Like on position 0 inside few customer views are there I am rendering via loop – Jhaman Das Feb 23 '22 at 18:15