0

Ok, so i`m trying to get the scrollY atribute from my RecycleView (to see the position of where i am scrolling in the CardList. I have the following setup

First, the main activity , which houses a SlidingTabs layout, which has a SwipeToFresh layout as he child and a ViewPager for the 2 fragments of the tabs, like this :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.popal.soul.MovieListActivity">

<com.example.popal.soul.SlidingTabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="2dp"
android:background="@color/ColorPrimary"/>


<android.support.v4.widget.SwipeRefreshLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/swipeContainer"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:layout_weight="1">

    </android.support.v4.view.ViewPager>

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


</LinearLayout>

Then i have each fragment with an identical layout, like this, with an RecycleView :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">

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

    <ProgressBar
        android:id="@+id/progress_bar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true" />
</RelativeLayout>

And finally the layout for each card of the RecycleView (i don't think it matters, but i'll add it anyway)

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
card_view:cardCornerRadius="4dp"
android:layout_margin="5dp">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="?android:attr/selectableItemBackground">
    <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="20dp"
        android:background="@color/bkg_card"
        android:text="Movie Title"
        android:gravity="center_vertical"
        android:textColor="@android:color/white"
        android:textSize="14dp"/>

    <ImageView
        android:elevation="5dp"
        android:scaleType="fitCenter"
        android:layout_width="100dp"
        android:layout_height="150dp"
        android:id="@+id/imageViewPoster"
        android:text="Poster"
        android:gravity="center_vertical"
        android:layout_below="@id/title"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="5dp"/>

    <ImageView
        android:id="@+id/imageViewFanart"
        android:scaleType="fitCenter"
        android:layout_alignBottom="@+id/imageViewPoster"
        android:layout_toEndOf="@+id/imageViewPoster"
        android:layout_width= "235dp"
        android:layout_height="122dp"
        android:layout_marginRight="5dp"/>

 </RelativeLayout>

 </android.support.v7.widget.CardView>

Now, i am trying to get the scrolling position of the RecycleView items from the main activity, where the SwipeToRefresh layout is defined. I need to do this to implement this workaround regarding a bug in the SwipeToRefresh layout, where trying to scroll up from the bottom of the list will trigger the refresh call : SwipeRefreshLayout + WebView when scroll position is at top , but as you can see, both solutions require access to the Swipe Container and RecycleView, in the same class or activity.

I tried using something like int scrollY = pager.getChildAt(pager.getCurrentItem()).getScrollY();

Which should theoretically return the position of the child item (in this case, the RecycleView) , but it still returns 0 (added log entries to get real-time events when the scrolling state has change), like if was still returning the position of the ViewPage adapter.

Anybody have an ideea?

Community
  • 1
  • 1
Alex
  • 127
  • 3
  • 12
  • 1
    You could define a simple listener interface that the hosting activity implements and through which the individual fragments can then pass their `RecyclerView`'s scroll position. This concept is described [here in more detail with some sample code](http://developer.android.com/training/basics/fragments/communicating.html). – MH. Sep 26 '15 at 21:43
  • You're right, a combination of you're answer + some modifications is exactly what i need. See, it's not `getScrollY();` that returns the position in the list, but the listener `OnScrollListener` for the RecycleView, which i can then override the `public void onScrolled(RecyclerView recycleList, int dx, int dy)` method inside it , wih this `int position = ((LinearLayoutManager) recycleList.getLayoutManager()).findFirstVisibleItemPosition();` . position will return the current scrolling position, and combined with your answer, i should be able to get it into my main activity. Thanks a lot. – Alex Sep 27 '15 at 21:45
  • You can add you comment as an answer when you have time, so i can mark the question as answered so you can get the reputation points. Thanks a lot again. – Alex Sep 27 '15 at 21:47
  • Whoops, totally forgot about this one. I've added the gist of the original comment as answer. Feel free to update with some of the code you ended up with if you feel that may be helpful to others. – MH. Oct 06 '15 at 08:37

1 Answers1

1

As per earlier comment: You could define a simple listener interface that the hosting activity implements and through which the individual fragments can then pass their RecyclerView's scroll position (from their own local OnScrollListener).

This concept of communicating with fragments is described here in more detail with some sample code‌​.

MH.
  • 45,303
  • 10
  • 103
  • 116