Recycleview scroll to a position not working inside Nestedscrollview
- Darshana
- 2018-05-22 09:37
- 5
I have implemented a recycleview inside a nested scroll view. But recycle view scroll to position methods are not working.
Below is my sample code
<?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.NestedScrollView 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:layout_width="match_parent" android:layout_height="wrap_content" android:paddingBottom="@dimen/activity_vertical_margin" app:layout_behavior="@string/appbar_scrolling_view_behavior"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <android.support.v7.widget.RecyclerView android:id="@+id/list_view" android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_behavior="@string/appbar_scrolling_view_behavior"/> </LinearLayout> </android.support.v4.widget.NestedScrollView>
below is the method for scrolling
RecyclerView.SmoothScroller smoothScroller = new LinearSmoothScroller(this) { @Override protected int getVerticalSnapPreference() { return LinearSmoothScroller.SNAP_TO_START; } }; smoothScroller.setTargetPosition(pos); recyclerView.getLayoutManager().startSmoothScroll(smoothScroller);
5 Answers
<android.support.design.widget.CoordinatorLayout 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:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" tools:context=".MainActivity"> <android.support.design.widget.AppBarLayout android:id="@+id/appBar" android:layout_width="match_parent" android:layout_height="300dp" android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" android:fitsSystemWindows="true"> <android.support.design.widget.CollapsingToolbarLayout android:id="@+id/collapsingToolbar" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_scrollFlags="scroll|exitUntilCollapsed" android:fitsSystemWindows="true" app:contentScrim="?attr/colorPrimary" app:expandedTitleMarginStart="48dp" app:expandedTitleMarginEnd="64dp" app:title="Scroll"> <ImageView android:id="@+id/toolbarImage" android:layout_width="match_parent" android:layout_height="match_parent" android:scaleType="centerCrop" android:fitsSystemWindows="true" android:src="@mipmap/ic_launcher" app:layout_collapseMode="parallax" /> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" app:popupTheme="@style/ThemeOverlay.AppCompat.Light" app:layout_collapseMode="pin" /> </android.support.design.widget.CollapsingToolbarLayout> </android.support.design.widget.AppBarLayout> <android.support.v7.widget.RecyclerView app:layout_behavior="@string/appbar_scrolling_view_behavior" android:id="@+id/recycler_view" android:scrollbars="vertical" android:nestedScrollingEnabled="false" android:layout_width="match_parent" android:layout_height="wrap_content"/>
Remove the NestedScrollView and add
app:layout_behavior="@string/appbar_scrolling_view_behavior"
to your Recyclerview.Then ScrollToPosition works fine.
android-recyclerview inside nestedscrollview scroll to position, do not use recyclerView inside NestedScrollView. it may cause cascading problems! I suggest using ItemViewTypes in RecyclerView for handling multiple kinds of views. just add a RecyclerView with match_parent width and height. then in your recyclerViewAdapter override getItemViewType and use position for handling what layout to be inflated. after that you can handle your view holder by using onBindViewHolder method. Need to scroll ScrollView by getting top position of RecyclerView's child : recyclerView.post(() -> { float y = recyclerView.getY() + recyclerView.
Dino Sunny
2018-05-30 12:25
The thing is that you need to scroll the NestedScrollView, not the RecyclerView. E.g:
final float y = recyclerView.getChildAt(selectedItem.getPos()).getY(); scrollView.smoothScrollTo(0, y)
recyclerview inside viewpager not scrolling, I have a RecyclerView inside a ViewPager that only occupies the bottom half of the screen, and what I want to do is have the entire screen scroll if the RecyclerViews received a vertical scroll event. UI hierarchy in a nutshell: try to set onTouchListener for recycleView: recycleView.setOnTouchListener(new OnTouchListener() { private float mLastX = 0, mLastY = 0; @Override public
Gabor
2019-04-25 05:08
Try this,
new Handler().postDelayed(new Runnable() { @Override public void run() { recyclerview.scrollToPosition(position); } }, 200);
nested scrollview not scrolling android, Now when I scroll the view the view is not scrolling. But since the layout is placed inside the Cordinator layout its moving up till the ToolBar is hid. But its not scrolling up. Here is my main activity xml, The view pager is inside a tabbed layout. Reason for that is that if the scrollview's child is the same size as the scrollview itself (both match_parent for height) it means that there is nothing to scroll through, since they are of same size and the scrollview will only be as high as the screen.
hasan_shaikh
2018-05-22 09:42
Use this:
yourRecyclerView.setNestedScrollingEnabled(false);
It may solve your problem.
recyclerview inside nestedscrollview not recycling, NestedScrollView draws all child element, So if RecyclerView has been used inside NestedScrollView all the element of adapter will be loaded at first time itself. I think the best way to resolve this issue is to your profileCardview as a header in your recycler view and then remove the nested scroll view.
Anton Prokopov
2018-05-22 09:42
This is how I resolve this issue
first get recycle view position that need to scroll using below method
Then scroll nested scroll view to that position
nestedScrollingView.post(new Runnable() { @Override public void run() { nestedScrollingView.fling(0); nestedScrollingView.smoothScrollTo(0, (int) y); } });
Don't forget to add
android:fillViewport="true"
on nestedscrollviewRecycleview scroll to a position not working inside Nestedscrollview, This is how I resolve this issue. first get recycle view position that need to scroll using below method final float y = recyclerView. This is how I resolve this issue. first get recycle view position that need to scroll using below method final float y = recyclerView.getChildAt(selectedItem.getPos())
Darshana
2018-06-25 07:04