1

I have a Recyclerview inside Scrollview, I want to store item details of Recyclerview where scroll stopped.

I have a xml like,

<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/White">

<RelativeLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v4.view.ViewPager
        android:id="@+id/tile_view_pager"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:visibility="gone"
        android:layout_marginRight="2dp"/>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/news_feed_recycler"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
       />

</RelativeLayout>

I have added ScrollListener for recyclerview. But As I'm using parent Scrollview for the activity, recyclerview's ScrollListener is not getting called. I tried changing parent scrollview to NestedScrollView then recyclerview is scrolling as a one item at once but i want scroll to be freely. Like scrolling our contacts.

Issue : When I scroll, Parent ScrollView getting scrolled and I don't have any control on my recyclerview. So It's difficult to figure out which item of recyclerview is visible once scroll stopped.

Basically I want to code without changing my XML.

Community
  • 1
  • 1
Amresh Ranjan
  • 159
  • 1
  • 1
  • 8

4 Answers4

1

I want to store item details of Recyclerview where scroll stopped.

When scroll stops, scroll state transitions into idle state. However, idle state is the state before and after scroll. Therefore you are looking for a transition into idle state after settling state.

Do it like this:

mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
    boolean scrolled;
    int[] displayedPositions;

    @Override
    public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
        super.onScrollStateChanged(recyclerView, newState);
        if (newState == RecyclerView.SCROLL_STATE_SETTLING) {
            scrolled = true;
        } else if (newState == RecyclerView.SCROLL_STATE_IDLE && scrolled) {
            scrolled = false;
            int[] into = new int[//number of spans];
            displayedPositions = ((StaggeredGridLayoutManager) recyclerView.getLayoutManager()).findFirstCompletelyVisibleItemPositions(into)
        }
    }
}
kalabalik
  • 3,792
  • 2
  • 21
  • 50
  • RecyclerView.OnScrollListener is not getting called as I'm using ScrollView as a parent view. – Amresh Ranjan Jan 17 '18 at 10:08
  • Use `NestedScrollView` instead of `ScrollView` and add `android:nestedScrollingEnabled="false"` to the XML of your `RecyclerView`. This will, however, break view recycling. – kalabalik Jan 17 '18 at 10:13
0
  private int overallXScrol = 0;

//...
mRecyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);

            overallXScroll = overallXScroll + dx;

            Log.i("check","overall->" + overallXScroll);

        }
    });
  • This code will never execute, i have tested. It will not execute the Scroll listener of recyclerview because we have parent scrollview. – Amresh Ranjan Jan 17 '18 at 08:00
  • add this line : recycler_view.setNestedScrollingEnabled(false); and i have edited my answer , try it once.may it helps you – chandani c patel Jan 17 '18 at 08:03
  • Not Working, onScrolled will called only once when recyclerview is populating. I want every scroll to be captured. I have tried your way. It's not working. – Amresh Ranjan Jan 17 '18 at 08:19
  • have a look to this link : https://stackoverflow.com/questions/26370289/snappy-scrolling-in-recyclerview – chandani c patel Jan 17 '18 at 08:29
0

try NestedScrollView instead scrollview like below

 <android.support.v4.widget.NestedScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/White">

<RelativeLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v4.view.ViewPager
        android:id="@+id/tile_view_pager"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:visibility="gone"
        android:layout_marginRight="2dp"/>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/news_feed_recycler"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
       />

</RelativeLayout>
Omkar
  • 3,040
  • 1
  • 22
  • 42
0

I found the solution. Just we need to change layout. I'm using CoordinatorLayout as a parent view. And Now I'm able to use onScrollListener of recyclerview.

<android.support.design.widget.CoordinatorLayout    
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/White">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.design.widget.CollapsingToolbarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

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

                <android.support.v4.view.ViewPager
                    android:id="@+id/tile_view_pager"
                    android:layout_width="match_parent"
                    android:layout_height="200dp"
                    android:layout_marginEnd="2dp"
                    android:visibility="gone"/>              

            </LinearLayout>

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

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

    <android.support.v7.widget.RecyclerView
        android:id="@+id/news_feed_recycler"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
</android.support.design.widget.CoordinatorLayout>
Amresh Ranjan
  • 159
  • 1
  • 1
  • 8