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.