3

I have RecyclerView which its height is set to wrap_content. Now I need to implement OnLoadMore to it but there is a problem. I used,

RecyclerView.OnScrollListener.onScrolled(RecyclerView recyclerView, int dx, int dy)

But it doesn't get invoked because my RecyclerView doesn't scroll. Its height is wrap_content.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/root_rtl"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white">

    <include
        android:id="@+id/tool_bar"
        layout="@layout/toolbar" />

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/tool_bar">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            tools:context="com.yarima.msn.Activities.ProfileActivity">

            <FrameLayout
                 android:id="@+id/FRAGMENT_PLACEHOLDER"
                 android:layout_width="match_parent"
                 android:layout_height="wrap_content"
                 android:background="@color/white">

                 <!-- Some Content That I want to scroll with recyclerview -->
            </FrameLayout>

            <android.support.v7.widget.RecyclerView
                 android:id="@+id/recyclerview_posts"
                 android:layout_width="match_parent"
                 android:layout_height="wrap_content"
                 android:scrollbars="vertical"
                 android:bellow="@id/FRAGMENT_PLACEHOLDER"/>
        </RelativeLayout>
    </ScrollView>

</RelativeLayout>

So I need to use another approach for loading more pages to RecyclerView. I think the best way to do this, is calling onLoadMore event when the last item of RecyclerView become visible. I already tried to do this from onBindViewHolder method in adapter, but all pages loaded altogether.

if(getItemCount()-position == 1 && onLoadMoreListener != null){
    if (recyclerView != null) {
        visibleItemCount = recyclerView.getChildCount();
        totalItemCount = recyclerView.getLayoutManager().getItemCount();
        firstVisibleItem = ((LinearLayoutManager)recyclerView.getLayoutManager()).findFirstVisibleItemPosition();
        if (loading) {
            if (totalItemCount > previousTotal) {
                loading = false;
                previousTotal = totalItemCount;
            }
        }
        if (!loading && (totalItemCount - visibleItemCount)
                <= (firstVisibleItem + visibleThreshold)) {
            loading = true;
            onLoadMoreListener.onLoadMore();
        }
    }
}

What is the alternative way to implement onLoadMore without using scroll events?

Update:

The RecyclerView works perfectly with android:layout_height:"wrap_content" and my ScrollView scrolls smoothly.

Update 2:

My problem is when your RecyclerView height is wrap_content, scroll events of RecyclerView cannot be invoked. So I need an alternative way to find out when my RecyclerView reaches to end of its list and implement OnLoadMore event that way.

Update 3

I simplified xml before I wrote it in question... In real xml, there is ViewPager instead of the RecyclerView. And I have 4 tabs in that ViewPager that each tab contains a RecyclerView with different contents.

Above of this ViewPager I have some information about user and I want to scroll all of them together. So I put this header and ViewPager in a ScrollView and set the height of RecyclerView to wrap_content.

You can take a look at profile page of instagram. I want to this page works like that.

It's not possible to show this information in header of RecyclerView because in this way, I should add this information in each RecyclerView in every tabs.

Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
Saman Salehi
  • 1,995
  • 1
  • 22
  • 36
  • what is support library android version you are using ? – Harshad Pansuriya Dec 12 '16 at 11:25
  • @Ironman 24.2.1 – Saman Salehi Dec 12 '16 at 11:26
  • Why do you need the `RecyclerView` height to be `wrap_content` anyway? – Reaz Murshed Dec 14 '16 at 10:32
  • @ReazMurshed: I simplified xml before I wrote it in question... In real xml, there is `ViewPager` instead of the `RecyclerView`. And I have 4 tabs in that `ViewPager` that each tab contains a `RecyclerView` with different contents. above of this `ViewPager` I have some information about user and I want to all this page scroll togather so I put this header and `ViewPager` in a `ScrollView` and set the height of `RecyclerView` to `wrap_content`... You can take a look at profile page of instagram. I want to this page works like that. – Saman Salehi Dec 14 '16 at 10:57
  • Set the information about user as a header of the `RecyclerView`. Simpler approach. http://stackoverflow.com/a/31154402/3145960 – Reaz Murshed Dec 14 '16 at 19:55
  • @ReazMurshed It's not possible because this `RecyclerView` and 3 others are in a `ViewPager`. Each `RecyclerView` in a tab of `ViewPager`. So if I want to show this information in header of `RecyclerView`, I should add it in every `RecyclerView`s in different pages of `ViewPager`. – Saman Salehi Dec 15 '16 at 10:02
  • Hmm... Now I get it. I'll get an answer to your question tonight I hope. Please have patience. – Reaz Murshed Dec 15 '16 at 11:20
  • @ReazMurshed Ok... Thanks – Saman Salehi Dec 15 '16 at 15:36

2 Answers2

0

Edited

Could look for scroll events in scrollview

https://developer.android.com/reference/android/view/View.html#onScrollChanged(int, int, int, int)

Mohammad Abbas
  • 245
  • 2
  • 13
0

You need to keep your ViewPager and your RecyclerView inside a NestedScrollView. The final xml should look like this.

<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ViewPager/>
    <RecyclerView/>

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

And set the height of your RecyclerView to match_parent.

You'll face another problem here. The page will automatically scroll to bottom when the RecyclerView will be loaded. But there's a solution to it too.

Adding android:descendantFocusability="blocksDescendants" to the child layout in NestedScrollView which will prevent the automatic scrolling to the bottom.

If the above doesn't work, try setting nestedScrollView.scrollTo(0, 0); from your code after the RecyclerView is loaded with items.

Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98