Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scroll to position in recyclerview inside NestedScrollView?

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();
}
like image 210
Vishal Vaishnav Avatar asked May 16 '18 09:05

Vishal Vaishnav


People also ask

What is nested scrolling in Recyclerview?

NestedScrollView is just like ScrollView, but it supports acting as both a nested scrolling parent and child. In your case you have to define your own scrolling behaviour.

How do I prevent NestedScrollView from scrolling if body is small?

The problem can be solved by moving the SliverAppBar into the CustomScrollView and not use the NestedScrollView at all.

Why we use nested scroll view?

NestedScrollView is used when there is a need for a scrolling view inside another scrolling view.


1 Answers

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);
});
like image 159
himangi Avatar answered Oct 07 '22 19:10

himangi